`

Flex 自定义事件

    博客分类:
  • Flex
 
阅读更多

Flex 自定义事件

 

public static const dispatcher…”,就是声明一个公有的静态对象,这样才使的可以在任意地方访问到并且对他进行抛出和侦听事件。
自定义CustomEvent类,继承Event,并且给他增加一个dispatcher:EventDispatcher属性。
使用这个类,就可以抛出事件并附带数据。
因为使用了 static 设置为静态属性,所以其他任意地方只要注册有这个事件侦听,就能捕获到。

CustomEvent:
package
{
import flash.events.Event;
import flash.events.EventDispatcher;

public class CustomEvent extends Event
{
public static const EVENT_NAME:String=”event_name”;

public static const dispatcher:EventDispatcher=new EventDispatcher();
public var data:Object;

public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false, data:Object=null)
{
super(type, bubbles, cancelable);
this.data=data;
}
override public function clone():Event{
return new CustomEvent(type, bubbles, cancelable, data);
}
}
}

抛出事件时:
var custom:CustomEvent=new CustomEvent(CustomEvent.CLICK);
//custom.data is Object;//发送事件同时,可以传值(可传可不传)
CustomEvent.dispatcher.dispatchEvent(custom);

侦听事件时:
CustomEvent.dispatcher.addEventListener(CustomEvent.CLICK,clickHandler);

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics