加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:44:07 所属栏目:百科 来源:网络整理
导读:今天在写事件监听器时,想往处理函数中传入参数。上网查到了一篇不错的文章,如下: 注:CSDN已经有人转载了,但是我还是贴到了自己的日志里,一方面是方便以后参与,一方面可能方便我的朋友们,或来其它同行。 package { ??? public class EventArgExtend ?

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读