Flex4 学习笔记-Action Script基础语法-在Action Script定义事件
这次实例,是在Action Script 类中.定义一个事件..然后也是通过点击按钮.触发事件..
并且.访问Action Script类中的变量值.
?
下面是具体代码实现:
第一步:
在Flex项目中,添加一个文件夹..然后添加一个Action Script类. 输入类名.然后继承至flash.events.Event;
点击确认.
package event{import flash.events.Event;/* *自定义Event事件类; * */public class EventAction extends Event{public static const EVENTTYPE:String = "event";private var param:int;public function EventAction(type:String,bubbles:Boolean=false, cancelable:Boolean=false){super(type, bubbles, cancelable);}//获取param变量的值;public function getParamMethod():int{return this.param;}//设置param变量的值;public function setParamMethod(param:int):void{this.param = param;}}}
?
?
第二步:
在新建一个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" creationComplete="application1_creationCompleteHandler(event)" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"><s:layout><s:HorizontalLayout verticalAlign="middle" horizontalAlign="center"/></s:layout><fx:Script><![CDATA[import event.EventAction;import mx.events.FlexEvent;<!--按钮事件;-->protected function btnID_clickHandler(event:MouseEvent):void{//实例化一个对象EventAction对象;var eventAction:EventAction = new EventAction(EventAction.EVENTTYPE);//设置值;eventAction.setParamMethod(200);//分发event;this.dispatchEvent(eventAction);}//加载事件;protected function application1_creationCompleteHandler(event:FlexEvent):void{// TODO Auto-generated method stubthis.addEventListener(EventAction.EVENTTYPE,eventFunction);}//监听方法;private function eventFunction(e:EventAction):void{//获取eventAction类中的param变量值;var param:int = e.getParamMethod();//输出:trace("输出param值:"+param);}]]></fx:Script><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --></fx:Declarations><s:Button id="btnID" label="触发自定义事件" click="btnID_clickHandler(event)"></s:Button></s:Application>
?
?
最后运行:
应用程序.则会在后台输出:
输出param值:200
?