主頁(yè) > 知識(shí)庫(kù) > 在as中監(jiān)聽(tīng)自定義事件并處理事件的實(shí)例代碼

在as中監(jiān)聽(tīng)自定義事件并處理事件的實(shí)例代碼

熱門標(biāo)簽:什么行業(yè)需要電話機(jī)器人 天津企業(yè)外呼系統(tǒng)代理商 外呼系統(tǒng)號(hào)碼顯示 地圖標(biāo)注多家店 無(wú)錫電銷外呼系統(tǒng)代理 中山外呼系統(tǒng)中間件 車載電話機(jī)器人 南昌crm外呼系統(tǒng)如何 廣州ai外呼系統(tǒng)業(yè)務(wù)
場(chǎng)景描述:點(diǎn)擊一張圖片,響應(yīng)事件。必須在AS中,去監(jiān)聽(tīng)事件,并處理事件。
1 自定義了一個(gè)事件,如下:
復(fù)制代碼 代碼如下:

package bridge {
import flash.events.Event;
import mx.events.FlexEvent;
public class MyEvent extends Event {
public static const myclick:String="myclick";
public function MyEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}
}

2 監(jiān)聽(tīng)事件處理的AS類,如下:
復(fù)制代碼 代碼如下:

package handler
{
import bridge.MyEvent;
import flash.events.Event;
import mx.controls.Alert;
import skin.ImgHanderSkin;
import spark.components.BorderContainer;
import spark.components.Image;
public class ImgHander extends BorderContainer
{
[SkinPart(required="true")]
public var img:Image;// 打開(kāi)按鈕
public function ImgHander()
{
super();
this.setStyle("skinClass",ImgHanderSkin);
this.percentHeight=100;
this.percentWidth=100;
}
//初始化監(jiān)聽(tīng)
override public function initialize():void{
super.initialize();
img.addEventListener(MyEvent.myclick,SetImgShouZhanUrl);
}
private function SetImgShouZhanUrl(event:Event):void {
Alert.show("Preview");
}
}
}

3 新建MXML外觀,皮膚類,在這里發(fā)送自定義事件。(在assert文件夾下有一張圖片哦:柯南.jpg)如下:
復(fù)制代碼 代碼如下:

?xml version="1.0" encoding="utf-8"?>
!--
ADOBE SYSTEMS INCORPORATED
Copyright 2008 Adobe Systems Incorporated
All Rights Reserved.
NOTICE: Adobe permits you to use, modify, and distribute this file
in accordance with the terms of the license agreement accompanying it.
-->
!--- The default skin class for a Spark SkinnableContainer container.
@see spark.components.SkinnableContainer
@langversion 3.0
@playerversion Flash 10
@playerversion AIR 1.5
@productversion Flex 4
-->
s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:fb="http://ns.adobe.com/flashbuilder/2009" alpha.disabled="0.5">
fx:Metadata>[HostComponent("spark.components.BorderContainer")]/fx:Metadata>
fx:Script fb:purpose="styling">
![CDATA[
import bridge.MyEvent;
/**
* @private
*/
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) : void
{
// Push backgroundColor and backgroundAlpha directly.
// Handle undefined backgroundColor by hiding the background object.
if (isNaN(getStyle("backgroundColor")))
{
background.visible = false;
}
else
{
background.visible = true;
bgFill.color = getStyle("backgroundColor");
bgFill.alpha = getStyle("backgroundAlpha");
}
super.updateDisplayList(unscaledWidth, unscaledHeight);
}
private function img_mouseOutHandler(event:MouseEvent):void{
// TODO Auto-generated method stub
var e:MyEvent= new MyEvent(MyEvent.myclick);
img.dispatchEvent(e);
}
]]>
/fx:Script>
s:states>
s:State name="normal" />
s:State name="disabled" />
/s:states>
!--- Defines the appearance of the SkinnableContainer class's background. -->
s:Rect id="background" left="0" right="0" top="0" bottom="0">
s:fill>
!--- @private -->
s:SolidColor id="bgFill" color="#FFFFFF"/>
/s:fill>
/s:Rect>
!--
Note: setting the minimum size to 0 here so that changes to the host component's
size will not be thwarted by this skin part's minimum size. This is a compromise,
more about it here: http://bugs.adobe.com/jira/browse/SDK-21143
-->
!--- @copy spark.components.SkinnableContainer#contentGroup -->
s:Group id="contentGroup" left="0" right="0" top="0" bottom="0" minWidth="0" minHeight="0">
s:layout>
s:BasicLayout/>
/s:layout>
s:Image id="img" click="img_mouseOutHandler(event)" source="assert/柯南.jpg">
/s:Image>
/s:Group>
/s:Skin>

4 最后,創(chuàng)建一個(gè)MXML應(yīng)用程序里面,去調(diào)用,AS類。直接運(yùn)行。
復(fù)制代碼 代碼如下:

?xml version="1.0" encoding="utf-8"?>
s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600"
xmlns:handler="handler.*">
handler:ImgHander>
/handler:ImgHander>
/s:Application>

結(jié)束!
注意:
1 發(fā)送自定義事件:
復(fù)制代碼 代碼如下:

var e:MyEvent= new MyEvent(MyEvent.myclick);
img.dispatchEvent(e);

2 接收并處理事件:
img.addEventListener(MyEvent.myclick,SetImgShouZhanUrl);

標(biāo)簽:海西 佛山 仙桃 滄州 攀枝花 欽州 泰州 呂梁

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《在as中監(jiān)聽(tīng)自定義事件并處理事件的實(shí)例代碼》,本文關(guān)鍵詞  在,中,監(jiān)聽(tīng),自定義,事件,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《在as中監(jiān)聽(tīng)自定義事件并處理事件的實(shí)例代碼》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于在as中監(jiān)聽(tīng)自定義事件并處理事件的實(shí)例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章