您的位置:首页 > 其它

Flex addEventListener 添加事件时 传递参数并移除它

2011-11-18 18:00 399 查看
我们知道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

property
public var callee:Function


Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0, Flash Player 8, Flash Lite 4
A reference to the currently executing function.

这是他的原话,意思是一个对当前函数的引用。秀一下英语,比较难的时候,我都在google翻译过来,然后再去理解。

下面稍微修改一下,就可以移除了。

<?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想,看看能不能有什么突破点没。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: