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); }原文地址(虽然摘抄者也是转的,希望能说明出处啊):http://pengranxiang.javaeye.com/blog/640536