?
在已经了解了事件的机制,明白了事件的注册,侦听,触发过程后,再看自定义事件就比较简单了。示例如下:
先写自定义的事件类(继承Event类):
?
- package?events ?
- { ?
- ????import?flash.events.Event; ?
- ???? ?
- ????public?class?TransInfoEvent?extends?Event ?
- ????{ ?
- ????????public?var?username:String; ?
- ????????public?var?pwd:String; ?
- ???????? ?
-
????????public?static?var?Tag:String?=?"123"; ?
- ????????public?function?TransInfoEvent(type:String,?username:String,pwd:String) ?
- ????????{ ?
- ????????????super(type); ?
-
????????????this.username=username; ?
-
????????????this.pwd=pwd; ?
- ???????????? ?
- ????????} ?
- ????} ?
- }?
然后是事件的发送(红色字体内容)用dispatchEvent()方法:
- package?controls ?
- { ?
- ????import?events.TransInfoEvent; ?
- ???? ?
- ????import?flash.events.MouseEvent; ?
- ???? ?
- ????import?mx.events.FlexEvent; ?
- ????import?mx.managers.PopUpManager; ?
- ???? ?
- ????import?spark.components.Application; ?
- ????import?spark.components.Button; ?
- ????import?spark.components.TextInput; ?
- ???? ?
- ????public?class?RegistControl?extends?Application ?
- ????{ ?
- ????????public?var?username:TextInput; ?
- ????????public?var?pwd:TextInput; ?
- ???????? ?
- ????????public?var?submit1:Button; ?
- ????????public?var?submit2:Button; ?
- ???????? ?
- ????????public?var?parentView:Object; ?
- ????????public?var?callbackFunction:Function; ?
- ???????? ?
- ????????public?function?RegistControl() ?
- ????????{ ?
- ????????????super(); ?
- ????????????addEventListener(FlexEvent.CREATION_COMPLETE,creationCompleteHandler); ?
- ????????} ?
- ????????private?function?creationCompleteHandler(event:FlexEvent):void{ ?
- ????????submit1.addEventListener(MouseEvent.CLICK,submit1_clickHandler); ?
- ????????submit2.addEventListener(MouseEvent.CLICK,submit2_clickHandler); ?
- ????????} ?
-
????????private?function?submit1_clickHandler(event:MouseEvent):void{ ?
- ????????var?transEvent:TransInfoEvent=new?TransInfoEvent(TransInfoEvent.Tag,username.text,pwd.text); ?
- ????????dispatchEvent(transEvent); ?
- ????????PopUpManager.removePopUp(this); ?
- ????????} ?
- ????????private?function?submit2_clickHandler(event:MouseEvent):void{ ?
- ????????????callbackFunction(username.text,pwd.text); ?
- ????????????PopUpManager.removePopUp(this); ?
- ???????? ?
- ????????} ?
- ????} ?
- }?
事件监听(绿的字体部分):
- package?controls ?
- { ?
- ????import?events.TransInfoEvent; ?
- ???? ?
- ????import?flash.events.MouseEvent; ?
- ???? ?
- ????import?mx.core.IFlexDisplayObject; ?
- ????import?mx.events.FlexEvent; ?
- ????import?mx.managers.PopUpManager; ?
- ???? ?
- ????import?spark.components.Application; ?
- ????import?spark.components.Button; ?
- ????import?spark.components.TextInput; ?
- ???? ?
- ????public?class?IndexControl?extends?Application ?
- ????{ ?
- ????????public?var?login:LoginControl; ?
- ????????public?var?regist:RegistControl; ?
- ???????? ?
- ????????public?var?loginbutton:Button; ?
- ????????public?var?registbutton:Button; ?
- ???????? ?
- ????????public?var?username:TextInput; ?
- ????????public?var?pwd:TextInput; ?
- ???????? ?
- ????????public?function?IndexControl() ?
- ????????{ ?
- ????????????super(); ?
- ????????????addEventListener(FlexEvent.CREATION_COMPLETE,creationCompleteHandler); ?
- ????????} ?
- ????????private?function?creationCompleteHandler(event:FlexEvent):void{ ?
- ????????????regist.addEventListener(TransInfoEvent.Tag,regist_transInfoHandler); ?
- ????????????registbutton.addEventListener(MouseEvent.CLICK,regist_clickHandler); ?
- ????????????loginbutton.addEventListener(MouseEvent.CLICK,login_clickHandler); ?
- ????????} ?
-
????????private?function?regist_transInfoHandler(event:TransInfoEvent):void{ ?
- ????????????username.text=event.username; ?
-
????????????pwd.text=event.pwd;????????? ?
- ????????} ?
- ????????private?function?regist_clickHandler(event:MouseEvent):void{ ?
- ????????????showCompnent(regist); ?
-
????????????regist.parentView?=?this; ?
-
????????????regist.callbackFunction?=?callbackFunctionHandler; ?
- ????????} ?
- ????????private?function?login_clickHandler(event:MouseEvent):void{ ?
- ????????????showCompnent(login); ?
- ????????} ?
- ????????private?function?showCompnent(obj:IFlexDisplayObject):void{ ?
- ???????????? ?
- ????????????PopUpManager.addPopUp(obj,this,true); ?
- ????????????PopUpManager.centerPopUp(obj); ?
- ????????} ?
- ????????private?function?callbackFunctionHandler(name:String,password:String):void{ ?
-
????????????username.text=name; ?
-
????????????pwd.text=password;?? ?
- ???????? ?
- ????????} ?
- ????} ?
- }?
学习ING