Flex4中的自定义事件
1.方式一:通过继承Event类编写Flex4的自定义事件
下面我们来做一个例子,实现这样的功能:监控TextInput的输入,通过监控输入的内容,触发不同的自定义事件。
我们先来写这个自定义事件:
ActionScript类Test43Event.cs
package demo { import flash.events.Event; public class Test43Event extends Event { public static var Test43_Number:String = "number"; public static var Test43_Letter:String = "letter"; public static var Test43_Other:String = "other"; public function Test43Event(type:String, bubbles:Boolean=false, cancelable:Boolean=false) { super(type, bubbles, cancelable); } } }
然后添加一个MXML应用程序test43.mxml调用这个自定义事件
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Script> <![CDATA[ import mx.events.FlexEvent; protected function textinput1_keyDownHandler(event:KeyboardEvent):void { var test43Event:Test43Event; if(event.keyCode>=48 && event.keyCode<=57) //0-9 { test43Event = new Test43Event(Test43Event.Test43_Number); dispatchEvent(test43Event); } else if(event.keyCode>=65 && event.keyCode<=90) //a-z A-Z { test43Event = new Test43Event(Test43Event.Test43_Letter); dispatchEvent(test43Event); } else { test43Event = new Test43Event(Test43Event.Test43_Other); dispatchEvent(test43Event); } } private function func1(event:Event):void { if(event.type==Test43Event.Test43_Letter) { msg.text = "当前输入的是字母"; } else if(event.type==Test43Event.Test43_Number) { msg.text = "当前输入的是数字"; } else if(event.type==Test43Event.Test43_Other) { msg.text = "当前输入的是其他字符"; } } protected function application1_creationCompleteHandler(event:FlexEvent):void { this.addEventListener(Test43Event.Test43_Letter,func1); this.addEventListener(Test43Event.Test43_Number,func1); this.addEventListener(Test43Event.Test43_Other,func1); } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:TextInput id="text1" x="140" y="157" keyDown="textinput1_keyDownHandler(event)"/> <s:Label id="msg" x="140" y="128"/> </s:Application>
2.方式二:直接使用dispatchEvent编写Flex4的自定义事件
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Script> <![CDATA[ import mx.events.FlexEvent; protected function application1_creationCompleteHandler(event:FlexEvent):void { this.addEventListener("Letter",func1); this.addEventListener("Other",func1); } private function func1(event:Event):void { if(event.type == "Letter") { msg.text = "当前选择的是字母"; } else { msg.text = "当前选择的不是字母"; } } protected function textinput1_keyDownHandler(event:KeyboardEvent):void { if(event.keyCode>=65 && event.keyCode<=90) //a-z A-Z { dispatchEvent(new Event("Letter")); } else { dispatchEvent(new Event("Other")); } } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:TextInput x="180" y="179" keyDown="textinput1_keyDownHandler(event)"/> <s:Label x="179" y="143" id="msg"/> </s:Application>
参考资料:
Flex中事件的初探 http://www.blogjava.net/rainwindboys/archive/2008/08/07/220687.html
Flex各个keycode值对照 http://fengxiangpiao.javaeye.com/blog/802121