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

Flex addEventListener增多事件侦听函数时传递多个参数

2012-11-25 
Flex addEventListener增加事件侦听函数时传递多个参数今天在flex里通过addEventListener函数给控件动态加

Flex addEventListener增加事件侦听函数时传递多个参数

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

package{    public class EventArgExtend    {        public function EventArgExtend()        {        }                public static function create(f:Function,... arg):Function         {               var F:Boolean=false;               var _f:Function=function(e:*,..._arg)               {                   _arg=arg                   if(!F)                   {                       F=true                       _arg.unshift(e)                   }                   f.apply(null,_arg)               };               return _f;          }          public static function toString():String           {               return "Class JEventDelegate";          }    }}

?

使用的方式:

txtShow.addEventListener(MouseEvent.CLICK,EventArgExtend.create(clickHandler,1,"str"));            private function clickHandler(e:Event,...arg):void            {                Alert.show(arg[0].toString());                Alert.show(arg[1].toString());            }

?

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

?

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

?

?

?

1 楼 yunan246 2011-11-14   怎么remove掉啊?没有函数名,只有addEventListener,明摆着要内存泄漏。

热点排行