Flex4中的自定义事件(转)
发布时间:2020-12-15 01:11:20 所属栏目:百科 来源:网络整理
导读:1.方式一:通过继承Event类编写Flex4的自定义事件 下面我们来做一个例子,实现这样的功能:监控TextInput的输入,通过监控输入的内容,触发不同的自定义事件。 我们先来写这个自定义事件: ActionScript类Test43Event.cs 01 package demo 02 { 03 ? ? import
1.方式一:通过继承Event类编写Flex4的自定义事件 下面我们来做一个例子,实现这样的功能:监控TextInput的输入,通过监控输入的内容,触发不同的自定义事件。 我们先来写这个自定义事件: ActionScript类Test43Event.cs 01 package demo 02 { 03 ? ? import flash.events.Event; 04 ? ? ? 05 ? ? public class Test43Event extends Event 06 ? ? { 07 ? ? ? ? public static var Test43_Number:String = "number"; 08 ? ? ? ? public static var Test43_Letter:String = "letter"; 09 ? ? ? ? public static var Test43_Other:String = "other"; 10 ? ? ? ? ? 11 ? ? ? ? public function Test43Event(type:String,bubbles:Boolean=false,cancelable:Boolean=false) 12 ? ? ? ? { 13 ? ? ? ? ? ? super(type,bubbles,cancelable); 14 ? ? ? ? } 15 ? ? } 16 } ? 然后添加一个MXML应用程序test43.mxml调用这个自定义事件 01 <?xml version="1.0" encoding="utf-8"?> 02 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 03 ? ? ? ? ? ? ? ?xmlns:s="library://ns.adobe.com/flex/spark" 04 ? ? ? ? ? ? ? ?xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"creationComplete="application1_creationCompleteHandler(event)"> 05 ? ? <s:layout> 06 ? ? ? ? <s:BasicLayout/> 07 ? ? </s:layout> 08 09 ? ? <fx:Script> 10 ? ? ? ? <![CDATA[ 11 ? ? ? ? ? ? import mx.events.FlexEvent; 12 ? ? ? ? ? ? protected function textinput1_keyDownHandler(event:KeyboardEvent):void 13 ? ? ? ? ? ? { 14 ? ? ? ? ? ? ? ? var test43Event:Test43Event; 15 ? ? ? ? ? ? ? ? if(event.keyCode>=48 && event.keyCode<=57) ? ?//0-9 16 ? ? ? ? ? ? ? ? { 17 ? ? ? ? ? ? ? ? ? ? test43Event = new Test43Event(Test43Event.Test43_Number); 18 ? ? ? ? ? ? ? ? ? ? dispatchEvent(test43Event); 19 ? ? ? ? ? ? ? ? } 20 ? ? ? ? ? ? ? ? else if(event.keyCode>=65 && event.keyCode<=90) ? //a-z A-Z 21 ? ? ? ? ? ? ? ? { 22 ? ? ? ? ? ? ? ? ? ? test43Event = new Test43Event(Test43Event.Test43_Letter); 23 ? ? ? ? ? ? ? ? ? ? dispatchEvent(test43Event); 24 ? ? ? ? ? ? ? ? } 25 ? ? ? ? ? ? ? ? else 26 ? ? ? ? ? ? ? ? { 27 ? ? ? ? ? ? ? ? ? ? test43Event = new Test43Event(Test43Event.Test43_Other); 28 ? ? ? ? ? ? ? ? ? ? dispatchEvent(test43Event); 29 ? ? ? ? ? ? ? ? } 30 ? ? ? ? ? ? } 31 ? ? ? ? ? ? ? 32 ? ? ? ? ? ? private function func1(event:Event):void 33 ? ? ? ? ? ? { 34 ? ? ? ? ? ? ? ? if(event.type==Test43Event.Test43_Letter) 35 ? ? ? ? ? ? ? ? { 36 ? ? ? ? ? ? ? ? ? ? msg.text = "当前输入的是字母"; 37 ? ? ? ? ? ? ? ? } 38 ? ? ? ? ? ? ? ? else if(event.type==Test43Event.Test43_Number) 39 ? ? ? ? ? ? ? ? { 40 ? ? ? ? ? ? ? ? ? ? msg.text = "当前输入的是数字"; 41 ? ? ? ? ? ? ? ? } 42 ? ? ? ? ? ? ? ? else if(event.type==Test43Event.Test43_Other) 43 ? ? ? ? ? ? ? ? { 44 ? ? ? ? ? ? ? ? ? ? msg.text = "当前输入的是其他字符"; 45 ? ? ? ? ? ? ? ? } 46 ? ? ? ? ? ? } 47 48 ? ? ? ? ? ? protected function application1_creationCompleteHandler(event:FlexEvent):void 49 ? ? ? ? ? ? { 50 ? ? ? ? ? ? ? ? this.addEventListener(Test43Event.Test43_Letter,func1); 51 ? ? ? ? ? ? ? ? this.addEventListener(Test43Event.Test43_Number,func1); 52 ? ? ? ? ? ? ? ? this.addEventListener(Test43Event.Test43_Other,func1); 53 ? ? ? ? ? ? } 54 55 ? ? ? ? ]]> 56 ? ? </fx:Script> 57 58 ? ? <fx:Declarations> 59 ? ? ? ? <!-- 将非可视元素(例如服务、值对象)放在此处 --> 60 ? ? </fx:Declarations> 61 ? ? <s:TextInput id="text1" x="140" y="157" keyDown="textinput1_keyDownHandler(event)"/> 62 ? ? <s:Label id="msg" x="140" y="128"/> 63 </s:Application> ? ? 2.方式二:直接使用dispatchEvent编写Flex4的自定义事件 view sourceprint? 01 <?xml version="1.0" encoding="utf-8"?> 02 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 03 ? ? ? ? ? ? ? ?xmlns:s="library://ns.adobe.com/flex/spark" 04 ? ? ? ? ? ? ? ?xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"creationComplete="application1_creationCompleteHandler(event)"> 05 ? ? <s:layout> 06 ? ? ? ? <s:BasicLayout/> 07 ? ? </s:layout> 08 ? ? <fx:Script> 09 ? ? ? ? <![CDATA[ 10 ? ? ? ? ? ? import mx.events.FlexEvent; 11 12 ? ? ? ? ? ? protected function application1_creationCompleteHandler(event:FlexEvent):void 13 ? ? ? ? ? ? { 14 ? ? ? ? ? ? ? ? ? 15 ? ? ? ? ? ? ? ? this.addEventListener("Letter",func1); 16 ? ? ? ? ? ? ? ? this.addEventListener("Other",func1); 17 ? ? ? ? ? ? } 18 ? ? ? ? ? ? ? 19 ? ? ? ? ? ? private function func1(event:Event):void 20 ? ? ? ? ? ? { 21 ? ? ? ? ? ? ? ? if(event.type == "Letter") 22 ? ? ? ? ? ? ? ? { 23 ? ? ? ? ? ? ? ? ? ? msg.text = "当前选择的是字母"; 24 ? ? ? ? ? ? ? ? } 25 ? ? ? ? ? ? ? ? else 26 ? ? ? ? ? ? ? ? { 27 ? ? ? ? ? ? ? ? ? ? msg.text = "当前选择的不是字母"; 28 ? ? ? ? ? ? ? ? } 29 ? ? ? ? ? ? } 30 31 32 ? ? ? ? ? ? protected function textinput1_keyDownHandler(event:KeyboardEvent):void 33 ? ? ? ? ? ? { 34 ? ? ? ? ? ? ? ? if(event.keyCode>=65 && event.keyCode<=90) ? ?//a-z A-Z 35 ? ? ? ? ? ? ? ? { 36 ? ? ? ? ? ? ? ? ? ? dispatchEvent(new Event("Letter")); 37 ? ? ? ? ? ? ? ? } 38 ? ? ? ? ? ? ? ? else 39 ? ? ? ? ? ? ? ? { 40 ? ? ? ? ? ? ? ? ? ? dispatchEvent(new Event("Other")); 41 ? ? ? ? ? ? ? ? } 42 ? ? ? ? ? ? } 43 44 ? ? ? ? ]]> 45 ? ? </fx:Script> 46 ? ? <fx:Declarations> 47 ? ? ? ? <!-- 将非可视元素(例如服务、值对象)放在此处 --> 48 ? ? </fx:Declarations> 49 ? ? <s:TextInput x="180" y="179" keyDown="textinput1_keyDownHandler(event)"/> 50 ? ? <s:Label x="179" y="143" id="msg"/> 51 </s:Application> ? ? 参考资料: Flex中事件的初探 http://www.blogjava.net/rainwindboys/archive/2008/08/07/2 下面我们来做一个例子,实现这样的功能:监控TextInput的输入,通过监控输入的内容,触发不同的自定义事件。 我们先来写这个自定义事件: ActionScript类Test43Event.cs 01 package demo 02 { 03 ? ? import flash.events.Event; 04 ? ? ? 05 ? ? public class Test43Event extends Event 06 ? ? { 07 ? ? ? ? public static var Test43_Number:String = "number"; 08 ? ? ? ? public static var Test43_Letter:String = "letter"; 09 ? ? ? ? public static var Test43_Other:String = "other"; 10 ? ? ? ? ? 11 ? ? ? ? public function Test43Event(type:String,func1); 17 ? ? ? ? ? ? } 18 ? ? ? ? ? ? ? 19 ? ? ? ? ? ? private function func1(event:Event):void 20 ? ? ? ? ? ? { 21 ? ? ? ? ? ? ? ? if(event.type == "Letter") 22 ? ? ? ? ? ? ? ? { 23 ? ? ? ? ? ? ? ? ? ? msg.text = "当前选择的是字母"; 24 ? ? ? ? ? ? ? ? } 25 ? ? ? ? ? ? ? ? else 26 ? ? ? ? ? ? ? ? { 27 ? ? ? ? ? ? ? ? ? ? msg.text = "当前选择的不是字母"; 28 ? ? ? ? ? ? ? ? } 29 ? ? ? ? ? ? } 30 31 32 ? ? ? ? ? ? protected function textinput1_keyDownHandler(event:KeyboardEvent):void 33 ? ? ? ? ? ? { 34 ? ? ? ? ? ? ? ? if(event.keyCode>=65 && event.keyCode<=90) ? ?//a-z A-Z 35 ? ? ? ? ? ? ? ? { 36 ? ? ? ? ? ? ? ? ? ? dispatchEvent(new Event("Letter")); 37 ? ? ? ? ? ? ? ? } 38 ? ? ? ? ? ? ? ? else 39 ? ? ? ? ? ? ? ? { 40 ? ? ? ? ? ? ? ? ? ? dispatchEvent(new Event("Other")); 41 ? ? ? ? ? ? ? ? } 42 ? ? ? ? ? ? } 43 44 ? ? ? ? ]]> 45 ? ? </fx:Script> 46 ? ? <fx:Declarations> 47 ? ? ? ? <!-- 将非可视元素(例如服务、值对象)放在此处 --> 48 ? ? </fx:Declarations> 49 ? ? <s:TextInput x="180" y="179" keyDown="textinput1_keyDownHandler(event)"/> 50 ? ? <s:Label x="179" y="143" id="msg"/> 51 </s:Application> ? ? 参考资料: Flex中事件的初探 http://www.blogjava.net/rainwindboys/archive/2008/08/07/2 Flex各个keycode值对照 http://fengxiangpiao.javaeye.com/blog/802121 来源:http://www.cnblogs.com/modou/articles/1901038.html
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |