移除事件监听器<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

        

         对于任何一个处理器,如果不再使用就将它移除,是一个好主意。移除对象的引用,即可清理内存。你可以使用removeEventListener()方法来移除一个你不再需要的事件处理器。所有可以掉红addEventListener()方法的组件也可以调用removeEventListener()方法。removeEvenetListener()方法的语法如下:

 

componentInstance.removeEventListener(event_type:String, listener_function:Function,

use_capture:Boolean)

 

         考虑下面的代码:

 

myButton.removeEventListener(MouseEvent.CLICK, myClickHandler);

        

         event_typelistener_function参数是必须的。它们与addEventListener()方法的必须的参数相同。

 

         use_capture参数同样与addEventListener()方法中的use_capture参数相同。

 

         回想一下,你可以通过调用两次addEventListener()方法,一次将use_capture设置为true,另一次将use_capture设定为false,来对事件的各个阶段进行监听。要移除这两个监听器,就必须调用removeEventListener()方法两次:一次将use_capture参数设置为true,另一次将use_capture参数设定为false

 

         下面的简单程序展示了哪些类型的处理器可以被移除,哪些类型的不能被移除:

 

<!-- events/RemoveEventListenerExample.mxml -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

initialize="createHandler(event)">

<mx:Script><![CDATA[

import mx.controls.Alert;

private function createHandler(e:Event):void {

b1.addEventListener(MouseEvent.CLICK, myClickHandler);

}

private function removeMyHandlers(e:Event):void {

/* Remove listener for b1's click event because it was added

with the addEventListener() method. */

b1.removeEventListener(MouseEvent.CLICK, myClickHandler);

/* Does NOT remove the listener for b2's click event because it

was added inline in an MXML tag. */

b2.removeEventListener(MouseEvent.CLICK, myClickHandler);

}

private function myClickHandler(e:Event):void {

Alert.show("The button was clicked.");

}

]]></mx:Script>

<mx:Button id="b1" label="Click Me"/>

<mx:Button label="Click Me Too" id="b2" click="myClickHandler(event)"/>

<mx:Button label="Remove Event Listeners" id="b3" click="removeMyHandlers(event)"/>

</mx:Application>

 

创建事件处理器类

 

         你可以创建一个外部类文件将类中的方法作为事件处理器使用。对象本身不是事件处理器,但对象的方法可以是。定义一个处理所有事件的类,相同的事件处理逻辑就可以在程序中通用。这样,可以使增强你的MXML程序的可读性和可维护性。

 

         要创建一个类来处理事件,你通常需要引入flash.events.Event类。通常,你还要些一个空的构造函数。下面的ActionScript类文件中,每当handleAllEvents()方法处理一个事件的时候,就调用Alert控件的show()方法:

 

// events/MyEventHandler.as

package { // Empty package.

import flash.events.Event;

import mx.controls.Alert;

public class MyEventHandler {

public function MyEventHandler() {

// Empty constructor.

}

public function handleAllEvents(event:Event):void {

Alert.show("Some event happened.");

}

}

}

 

         在你的MXML文件中,定义一个MyEventHandler的实例,并且使用addEventHandler()方法注册为Button控件的点击事件的处理器。如下所示:

 

<?xml version="1.0"?>

<!-- events/CustomHandler.mxml -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="createHandler()">

<mx:Script><![CDATA[

private var myListener:MyEventHandler = new MyEventHandler();

private function createHandler():void {

b1.addEventListener(MouseEvent.CLICK, myListener.handleAllEvents);

}

]]></mx:Script>

<mx:Button label="Submit" id="b1"/>

</mx:Application>

 

         最好将事件处理方法定义为静态的,这样你就不需要实例化类。下面的createHandler()方法不需要实例化MyStaticEventHandler类,就可以注册handleAllEvents()方法:

 

<?xml version="1.0"?>

<!-- events/CustomHandlerStatic.mxml -->

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="createHandler()">

<mx:Script><![CDATA[

private function createHandler():void {

b1.addEventListener(MouseEvent.CLICK, MyStaticEventHandler.handleAllEvents);

}

]]></mx:Script>

<mx:Button label="Submit" id="b1"/>

</mx:Application>

 

         在文件中,你只是为方法片段加上了static关键字:

 

// events/MyStaticEventHandler.as

package { // Empty package.

import flash.events.Event;

import mx.controls.Alert;

public class MyStaticEventHandler {

public function MyStaticEventHandler() {

// Empty constructor.

}

public static function handleAllEvents(event:Event):void {

Alert.show("Some event happened.");

}

}

}

 

         将你的事件监听器类保存在源文件路径中。你也可以将ActionScript类保存在和MXML文件相同的目录中,尽管Adobe并不推荐这样做。