Flex4.5--Event(事件)
1 利用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="applicationCreateHandler()">
<fx:Script>
<![CDATA[
protected function button1_clickHandler():void{
dispatchEvent(new Event("Clicked"));
}
protected function applicationCreateHandler():void{
this.addEventListener("Clicked",clickedHandler);
}
private function clickedHandler(e:Event):void{
trace("Event Is Listened");
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Metadata>
[Event(name="Clicked",type="flash.events.Event")]
</fx:Metadata>
<s:Button x="344" y="194" id="button1" label="Click" click="button1_clickHandler()"/>
</s:Application>
2 利用Action Script 声明事件
package com.event
{
import flash.events.Event;
public class MyEvent extends Event
{
public static const MY_EVENT:String = "myEvent";
private var eventObject:int;
public function MyEvent(type:String, param:int, bubbles:Boolean=false, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
this.eventObject=param;
}
public function getEventObject():int{
return this.eventObject;
}
}
}
<?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="applicationHandler()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.event.MyEvent;
protected function buttonClickHandler():void{
var event:MyEvent = new MyEvent(MyEvent.MY_EVENT,2);
dispatchEvent(event);
}
protected function applicationHandler():void{
this.addEventListener(MyEvent.MY_EVENT,eventHandler);
}
private function eventHandler(e:MyEvent):void{
trace("参数: "+e.getEventObject());
}
]]>
</fx:Script>
<s:Button x="284" y="300" label="Button" click="buttonClickHandler()"/>
</s:Application>
本文介绍了Flex4.5中事件处理的两种方法:通过MXML声明事件和使用ActionScript自定义事件。通过实例展示了如何定义事件处理函数、监听事件及传递参数。
244

被折叠的 条评论
为什么被折叠?



