Flex addEventListener 添加事件时 传递参数并移除它
来源:互联网 发布:p2p局域网监控软件 编辑:程序博客网 时间:2024/06/05 05:13
我们知道FLEX添加监听事件,会经常使用到,但是常见的方法,并不能自定义参数。我收集到一个非常好的方法。可以在监听的时候,添加自己喜欢的参数。好吧,我们开始第一个
<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"><fx:Script><![CDATA[import mx.controls.Alert;public var count:int=0;protected function button1_clickHandler(event:MouseEvent):void{bt2.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{clickPar(event,20,30);});}protected function clickPar(event:MouseEvent,x:int,y:int):void{count++;ta.text+="被调用了"+count+"次"+"x:"+x+"y:"+y+"\n";}]]></fx:Script><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --></fx:Declarations><s:Button x="64" y="40" label="按钮1" click="button1_clickHandler(event)"/><s:Button x="63" y="97" label="按钮2" id="bt2"/><s:TextArea x="217" y="32" id="ta"/></s:Application>
点击按钮1,为按钮2,添加相应的监听事件,点击一次按钮一,将会增加一次对按钮2的监听。这样我们就可以实现监听事件的时候,传递相关参数。问题来了,我们现在需要移除监听。好吧,我们继续,按照我们常规的做法,就是把这个函数再写一遍就是了。
<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"><fx:Script><![CDATA[import mx.controls.Alert;public var count:int=0;protected function button1_clickHandler(event:MouseEvent):void{bt2.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{clickPar(event,20,30);});}protected function clickPar(event:MouseEvent,x:int,y:int):void{count++;ta.text+="被调用了"+count+"次"+"x:"+x+"y:"+y+"\n";}protected function button3_clickHandler(event:MouseEvent):void{bt2.removeEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{clickPar(event,20,30); });}]]></fx:Script><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --></fx:Declarations><s:Button x="64" y="40" label="按钮1" click="button1_clickHandler(event)"/><s:Button x="63" y="97" label="按钮2" id="bt2"/><s:TextArea x="217" y="32" id="ta"/><s:Button x="63" y="155" label="按钮3" click="button3_clickHandler(event)"/></s:Application>我们点击按钮三的时候,事实并非所想,难道这是一个bug,还是怎么回事?开始骂Adobe,设计的什么玩意。
其实function(event:MouseEvent):void
{
clickPar(event,20,30);
}
这是一个匿名函数,虽然写的是一模一样,但是函数地址是不一样的,所以说,移除的不是你添加的,这句话你明白了吗?我似乎听见,都在说,small case。
解铃还须系铃人,我们把那个监听的函数给记住,然后不就可以了吗!对。这个匿名函数,怎么能得到他呢。当然我们可以查一下帮助文档,arguments 出现在我们面前,为什么我会知道这个东西呢?当然,我写这篇文章之前,我都从网络上搜索好了,实现方法是什么,所以知道该查什么。
callee
propertypublic var callee:Function
Language Version: ActionScript 3.0Runtime Versions: AIR 1.0, Flash Player 8, Flash Lite 4A reference to the currently executing function.
下面稍微修改一下,就可以移除了。
<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"><fx:Script><![CDATA[import mx.controls.Alert;public var count:int=0;public var ft:Function;protected function button1_clickHandler(event:MouseEvent):void{bt2.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{clickPar(event,20,30,arguments.callee);});}protected function clickPar(event:MouseEvent,x:int,y:int,fn:Function):void{ft=fn;count++;ta.text+="被调用了"+count;}protected function button3_clickHandler(event:MouseEvent):void{bt2.removeEventListener(MouseEvent.CLICK,ft);}]]></fx:Script><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --></fx:Declarations><s:Button x="64" y="40" label="按钮1" click="button1_clickHandler(event)"/><s:Button x="63" y="97" label="按钮2" id="bt2"/><s:TextArea x="217" y="32" id="ta"/><s:Button x="63" y="155" label="按钮3" click="button3_clickHandler(event)"/></s:Application>
我总感觉,Flex与JS有太多的相似性,当我不熟悉Flex的时候,我就向JS想,看看能不能有什么突破点没。
- Flex addEventListener 添加事件时 传递参数并移除它
- as/flex 中添加事件addEventListener()时传递参数
- as/flex 中添加事件addEventListener()时传递参数
- Flex addEventListener传递参数
- Flex addEventListener增加事件侦听函数时传递多个参数
- Flex addEventListener增加事件侦听函数时传递多个参数
- Flex addEventListener增加事件侦听函数时传递多个参数
- Flex addEventListener增加事件侦听函数时传递多个参数
- Flex addEventListener增加事件侦听函数时传递多个参数
- Flex addEventListener增加事件侦听函数时传递多个参数
- Flex addEventListener增加事件侦听函数时传递多个参数
- Flex addEventListener增加事件侦听函数时传递多个参数 .
- 百度地图,移除添加的事件(addEventListener,removeEventListener)
- 如何UIButton添加事件并传递参数
- As3 addEventListener事件监听传递参数
- JS添加/绑定事件监听函数并传参数attachEvent和addEventListener兼容个浏览器
- 如何给组件添加事件并传递参数
- 循环addEventListener传递参数
- ORM
- Attribute "column" must be declared for element type "property"解决办法
- 敏捷开发般若敏捷系列之六:如何推广敏捷(下)(以无我之心,行无住之法)
- IP数据包分片理解
- 人情味,让你的内容脱颖而出
- Flex addEventListener 添加事件时 传递参数并移除它
- 汇编hello world!
- mysql的主从备份和双机互备实现
- java中的容器排序,TreeMap,List(笔记)
- 修改工程临时目录报错(VC++)
- 王晓东 石子合并
- 程序基础
- jquery 排除选择元素
- 文件上传:验证文件真实类型并返回文件路径