首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

flex里经过addEventListener指定回调函数时指定更多参数

2012-12-18 
flex里通过addEventListener指定回调函数时指定更多参数转载自:http://virgos.iteye.com/blog/480221?在fl

flex里通过addEventListener指定回调函数时指定更多参数

转载自:http://virgos.iteye.com/blog/480221

?

在flex里通过addEventListener函数给控件动态加载click事件侦听函数时,除了事件本身传递的Event类型参数外,还需要传递更多的参数,在网上找了一段代码,用起来还不错,张贴到这里。

   1. package     2. {     3.     public class EventArgExtend     4.     {     5.         public function EventArgExtend()     6.         {     7.         }     8.             9.         public static function create(f:Function,... arg):Function    10.         {    11.                var F:Boolean=false;    12.                var _f:Function=function(e:*,..._arg)    13.                {    14.                    _arg=arg    15.                    if(!F)    16.                    {    17.                        F=true    18.                        _arg.unshift(e)    19.                    }    20.                    f.apply(null,_arg)    21.                };    22.                return _f;    23.           }    24.           public static function toString():String    25.           {    26.                return "Class JEventDelegate";    27.           }    28.     }    29. }  

?=========================================== 使用的方式:
txtShow.addEventListener(MouseEvent.CLICK,EventArgExtend.create(clickHandler,1,"str"));

?

   1. private function clickHandler(e:Event,...arg):void     2. {     3.     Alert.show(arg[0].toString());     4.     Alert.show(arg[1].toString());     5. }  

?还有另外一个方法,没有封装效果,不过代码更加容易理解:

?

   1. var sayHello:String = "欢迎光临www.FlashJ.cn -Flash,Ria技术博客";     2. btn1.addEventListener(MouseEvent.CLICK,function (e:MouseEvent){clickHandlerWithArg(e,sayHello)});     3. function clickHandlerWithArg(e:MouseEvent,arg:String):void     4. {     5.     var out:String= e.target + "发出事件(有参数) :" + arg;     6.     trace(out);     7. }  
?

热点排行