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

来源:互联网 发布:玩网络枪战游戏很卡 编辑:程序博客网 时间:2024/05/16 00:45

今天在写事件监听器时,想往处理函数中传入参数。上网查到了一篇不错的文章,如下:

 

注:CSDN已经有人转载了,但是我还是贴到了自己的日志里,一方面是方便以后参与,一方面可能方便我的朋友们,或来其它同行。

 

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);
}

0 0