1.     为多个组件注册一个监听器 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

可以通过两种方式为一个事件定义多个处理器函数。当在MXML标签中定义事件时,你可以使用分号分隔多个处理器函数。下面的例子中为click事件添加了submitForm()debugMessage()函数;

 

<?xml version="1.0"?>

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

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

<mx:Script><![CDATA[

[Bindable]

private var s:String = "";

private function submitForm(e:Event):void {

// Handle event here.

s += "The submitForm() method was called. ";

}

private function debugMessage(e:Event):void {

// Handle event here.

s += "The debugMessage() method was called. ";

}

]]></mx:Script>

<mx:Button id="b1"

label="Do Both Actions"

click='submitForm(event); debugMessage(event);'

ADOBE FLEX 3 79

Adobe Flex 3 Developer Guide

/>

<mx:Label id="l1" text="{s}"/>

<mx:Button id="b2" label="Reset" click="s='';"/>

</mx:Application>

 

你可以使用addEventListener()方法添加任意数量的处理器。每个添加的处理器函数都将被调用。如下所示:

 

<?xml version="1.0"?>

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

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

creationComplete="createHandlers(event)">

<mx:Script><![CDATA[

[Bindable]

private var s:String = "";

public function createHandlers(e:Event):void {

b1.addEventListener(MouseEvent.CLICK, submitForm);

b1.addEventListener(MouseEvent.CLICK, debugMessage);

}

private function submitForm(e:Event):void {

// Handle event here.

s += "The submitForm() method was called. ";

}

private function debugMessage(e:Event):void {

// Handle event here.

s += "The debugMessage() method was called. ";

}

]]></mx:Script>

<mx:Button id="b1" label="Do Both Actions"/>

<mx:Label id="l1" text="{s}"/>

<mx:Button id="b2" label="Reset" click="s='';"/>

</mx:Application>

 

你可以混合使用将事件处理器添加到组件的方法:你可以在行内添加处理器,也可以使用addEvenntListener()方法。下面的例子在行内为按钮控件添加了一个点击事件处理器,然后又根据CheckBox控件的值有条件地添加了第二个点击处理器调用logAction()方法。

 

<?xml version="1.0"?>

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

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

<mx:Script><![CDATA[

import mx.controls.Alert;

private function initApp(e:Event):void {

cb1.addEventListener(MouseEvent.CLICK, handleCheckBoxChange);

b1.addEventListener(MouseEvent.CLICK, logAction);

}

private function handleCheckBoxChange(e:Event):void {

if (cb1.selected) {

b1.addEventListener(MouseEvent.CLICK, logAction);

ta1.text += "Added log listener." + "\n";

} else {

b1.removeEventListener(MouseEvent.CLICK, logAction);

ta1.text += "Removed log listener." + "\n";

}

}

private function performAction(e:Event):void {

Alert.show("You performed the action.");

}

private function logAction(e:Event):void {

ta1.text += "Action performed: " + e.type + ".\n";

}

]]></mx:Script>

<mx:Button label="Perform Action" id="b1" click="performAction(event)"/>

<mx:CheckBox id="cb1" label="Log?" selected="true"/>

<mx:TextArea id="ta1" height="200" width="300"/>

</mx:Application>

 

你可以在addEventListener()方法中使用priority参数来设置事件监听器被调用的顺序。

 

当使用行内的方式添加事件监听器时,你不能设置监听函数的优先级。