RemoveEventListener无法移除事件监听器的情况

小杜前几天问了我一个很基础的问题,居然就被问住了。

 

为什么removeEventListener不起效果,查了一下,原来对于MXML的属性中定义的事件监听无法用removeEventListener来移除的。以前看书居然没有看到,罪过罪过。

 

In the example below flex removeEventListener method working is demonstrated. This method removes the events from the flex components. Here mouse click event are removed by the method. In the example method doesn't able to remove the event from the button with id b3 because on this flex control, the associated event is built with the button click attribute.
And this button control tag is under mxml tags, this shows that events cannot be removed that are added inside the mxml tags.
 

 

 

<?xml version = '1.0' encoding = 'utf-8'?>
<mx:Application xmlns:mx = 'http://www.adobe.com/2006/mxml'
             initialize = 'addEvent();'>
  <mx:Script>
    import mx.controls.Alert;

    public function addEvent():void{
        b1.addEventListener(MouseEvent.CLICK, alertbox, false, 0)
    }

    public function removeEvent():void{ 
      b1.removeEventListener(MouseEvent.CLICK, alertbox)
      b2.removeEventListener(MouseEvent.CLICK, alertbox)
      b3.removeEventListener(MouseEvent.CLICK, alertbox)

    }

    public function alertbox(event:Event):void{
      Alert.show('Mouse click event occured');
    }
  
  </mx:Script>

  <mx:HBox>
  
    <mx:Button id = 'b1' label = 'b1 button'/>
    <mx:Button id = 'b2' label = 'b2 button
initialize = 'b2.addEventListener(MouseEvent.CLICK, alertbox, false,0);'/>
    <mx:Button id = 'b3' label = 'b3 button' click = 'alertbox(event);'/>
  
  </mx:HBox>

    <mx:TextInput text = 'click to remove click action
      click = 'removeEvent()' editable = 'false'/>

</mx:Application>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值