欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在as中监听自定义事件并处理事件的实例代码

程序员文章站 2022-06-19 12:59:44
场景描述:点击一张图片,响应事件。必须在as中,去监听事件,并处理事件。 1 自定义了一个事件,如下: 复制代码 代码如下: package bridge { import...
场景描述:点击一张图片,响应事件。必须在as中,去监听事件,并处理事件。
1 自定义了一个事件,如下:
复制代码 代码如下:

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 监听事件处理的as类,如下:
复制代码 代码如下:

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;// 打开按钮
public function imghander()
{
super();
this.setstyle("skinclass",imghanderskin);
this.percentheight=100;
this.percentwidth=100;
}
//初始化监听
override public function initialize():void{
super.initialize();
img.addeventlistener(myevent.myclick,setimgshouzhanurl);
}
private function setimgshouzhanurl(event:event):void {
alert.show("preview");
}
}
}

3 新建mxml外观,皮肤类,在这里发送自定义事件。(在assert文件夹下有一张图片哦:柯南.jpg)如下:
复制代码 代码如下:

<?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 最后,创建一个mxml应用程序里面,去调用,as类。直接运行。
复制代码 代码如下:

<?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>

结束!
注意:
1 发送自定义事件:
复制代码 代码如下:

var e:myevent= new myevent(myevent.myclick);
img.dispatchevent(e);

2 接收并处理事件:
img.addeventlistener(myevent.myclick,setimgshouzhanurl);