加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex中dispatchEvent的用法(自定义事件)

发布时间:2020-12-15 05:03:53 所属栏目:百科 来源:网络整理
导读:? 在已经了解了事件的机制,明白了事件的注册,侦听,触发过程后,再看自定义事件就比较简单了。示例如下: 先写自定义的事件类(继承Event类): ? package?events ? { ? ????import?flash.events.Event; ? ???? ? ????public?class?TransInfoEvent?extends

?

在已经了解了事件的机制,明白了事件的注册,侦听,触发过程后,再看自定义事件就比较简单了。示例如下:

先写自定义的事件类(继承Event类):

?

 
 
  1. package?events ?
  2. { ?
  3. ????import?flash.events.Event; ?
  4. ???? ?
  5. ????public?class?TransInfoEvent?extends?Event ?
  6. ????{ ?
  7. ????????public?var?username:String; ?
  8. ????????public?var?pwd:String; ?
  9. ???????? ?
  10. ????????public?static?var?Tag:String?=?"123"; ?
  11. ????????public?function?TransInfoEvent(type:String,?username:String,pwd:String) ?
  12. ????????{ ?
  13. ????????????super(type); ?
  14. ????????????this.username=username; ?
  15. ????????????this.pwd=pwd; ?
  16. ???????????? ?
  17. ????????} ?
  18. ????} ?
  19. }?

然后是事件的发送(红色字体内容)用dispatchEvent()方法:

 
 
  1. package?controls ?
  2. { ?
  3. ????import?events.TransInfoEvent; ?
  4. ???? ?
  5. ????import?flash.events.MouseEvent; ?
  6. ???? ?
  7. ????import?mx.events.FlexEvent; ?
  8. ????import?mx.managers.PopUpManager; ?
  9. ???? ?
  10. ????import?spark.components.Application; ?
  11. ????import?spark.components.Button; ?
  12. ????import?spark.components.TextInput; ?
  13. ???? ?
  14. ????public?class?RegistControl?extends?Application ?
  15. ????{ ?
  16. ????????public?var?username:TextInput; ?
  17. ????????public?var?pwd:TextInput; ?
  18. ???????? ?
  19. ????????public?var?submit1:Button; ?
  20. ????????public?var?submit2:Button; ?
  21. ???????? ?
  22. ????????public?var?parentView:Object; ?
  23. ????????public?var?callbackFunction:Function; ?
  24. ???????? ?
  25. ????????public?function?RegistControl() ?
  26. ????????{ ?
  27. ????????????super(); ?
  28. ????????????addEventListener(FlexEvent.CREATION_COMPLETE,creationCompleteHandler); ?
  29. ????????} ?
  30. ????????private?function?creationCompleteHandler(event:FlexEvent):void{ ?
  31. ????????submit1.addEventListener(MouseEvent.CLICK,submit1_clickHandler); ?
  32. ????????submit2.addEventListener(MouseEvent.CLICK,submit2_clickHandler); ?
  33. ????????} ?
  34. ????????private?function?submit1_clickHandler(event:MouseEvent):void{ ?
  35. ????????var?transEvent:TransInfoEvent=new?TransInfoEvent(TransInfoEvent.Tag,username.text,pwd.text); ?
  36. ????????dispatchEvent(transEvent); ?
  37. ????????PopUpManager.removePopUp(this); ?
  38. ????????} ?
  39. ????????private?function?submit2_clickHandler(event:MouseEvent):void{ ?
  40. ????????????callbackFunction(username.text,pwd.text); ?
  41. ????????????PopUpManager.removePopUp(this); ?
  42. ???????? ?
  43. ????????} ?
  44. ????} ?
  45. }?

事件监听(绿的字体部分):

 
 
  1. package?controls ?
  2. { ?
  3. ????import?events.TransInfoEvent; ?
  4. ???? ?
  5. ????import?flash.events.MouseEvent; ?
  6. ???? ?
  7. ????import?mx.core.IFlexDisplayObject; ?
  8. ????import?mx.events.FlexEvent; ?
  9. ????import?mx.managers.PopUpManager; ?
  10. ???? ?
  11. ????import?spark.components.Application; ?
  12. ????import?spark.components.Button; ?
  13. ????import?spark.components.TextInput; ?
  14. ???? ?
  15. ????public?class?IndexControl?extends?Application ?
  16. ????{ ?
  17. ????????public?var?login:LoginControl; ?
  18. ????????public?var?regist:RegistControl; ?
  19. ???????? ?
  20. ????????public?var?loginbutton:Button; ?
  21. ????????public?var?registbutton:Button; ?
  22. ???????? ?
  23. ????????public?var?username:TextInput; ?
  24. ????????public?var?pwd:TextInput; ?
  25. ???????? ?
  26. ????????public?function?IndexControl() ?
  27. ????????{ ?
  28. ????????????super(); ?
  29. ????????????addEventListener(FlexEvent.CREATION_COMPLETE,creationCompleteHandler); ?
  30. ????????} ?
  31. ????????private?function?creationCompleteHandler(event:FlexEvent):void{ ?
  32. ????????????regist.addEventListener(TransInfoEvent.Tag,regist_transInfoHandler); ?
  33. ????????????registbutton.addEventListener(MouseEvent.CLICK,regist_clickHandler); ?
  34. ????????????loginbutton.addEventListener(MouseEvent.CLICK,login_clickHandler); ?
  35. ????????} ?
  36. ????????private?function?regist_transInfoHandler(event:TransInfoEvent):void{ ?
  37. ????????????username.text=event.username; ?
  38. ????????????pwd.text=event.pwd;????????? ?
  39. ????????} ?
  40. ????????private?function?regist_clickHandler(event:MouseEvent):void{ ?
  41. ????????????showCompnent(regist); ?
  42. ????????????regist.parentView?=?this; ?
  43. ????????????regist.callbackFunction?=?callbackFunctionHandler; ?
  44. ????????} ?
  45. ????????private?function?login_clickHandler(event:MouseEvent):void{ ?
  46. ????????????showCompnent(login); ?
  47. ????????} ?
  48. ????????private?function?showCompnent(obj:IFlexDisplayObject):void{ ?
  49. ???????????? ?
  50. ????????????PopUpManager.addPopUp(obj,this,true); ?
  51. ????????????PopUpManager.centerPopUp(obj); ?
  52. ????????} ?
  53. ????????private?function?callbackFunctionHandler(name:String,password:String):void{ ?
  54. ????????????username.text=name; ?
  55. ????????????pwd.text=password;?? ?
  56. ???????? ?
  57. ????????} ?
  58. ????} ?
  59. }?

学习ING

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读