FLEX:自定义事件 总结
就几个步骤: 一,actionscript创建自定义事件: 1,当然是继承event 啦 2,构造函数 2 .1,事件属性:包括事件类型,还有事件的其它自定义属性 2.2,super() 3.重载clone()方法 public class EventLight extends event{ public var light:String; public function EventLight(eventName:String,selectedLight:String){ super(eventName); this.light=selectedLight; } override public function clone():Event{ return new eventLight(type,light); } } 二,使用元数据[Event]为组件定义事件 <mx:button lable="button1" click="light.text='blue' "> 这段代码中的click就是组件button中的内置事件属性, 要组件使用自定义事件,就得使用元数据[Event],方法 <fx:Metadata> [Event]( name="clickEvent" type="com.event.EventLight " ) </fx:Metadata> 三,触发事件的方法:dispatchEvent() 要触发事件,当然得创建事件对象的实例了 <script> ... public function clickEventHandler(selectedLight:String ):void { //下面的clickEvent就是刚才[Event]定义的事件 public var eventObj:EventLight= new EventLight ("clickEvent",selectedLight); dispatchEvent(eventObj); } ... </script> 四,侦听器 侦听器是什么?响应事件~~~什么意思?就是你想要事件触发后发现什么事啊,侦听器就是一个方法而已(不知道说得对不对。。。) 好了,通过上面的步骤,我假设了我已经为button定义了事件clickEvent并且button能使用此事件属性, 然后编写侦听器的方法 public function eventHandler(event:EventLight):void{ Button(event.target).label="what the fuck,you success to do that" } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |