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); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值