1. 向监听器函数传递额外参数<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
在添加监听器时,你可以向监听器函数传递额外的参数。如果你通过addEventListener()方法来添加监听器,你不能像监听器函数传递任何额外的参数,并且那个函数也只能定义一个参数,就是Event对象。例如,下面的代码将抛出一个错误,因为clickListener()方法需要两个参数:
<mx:Script>
public function addListeners():void {
b1.addEventListener(MouseEvent.CLICK,clickListener);
}
public function clickListener(e:MouseEvent, a:String):void { ... }
</mx:Script>
<mx:Button id="b1"/> |
因为addEventListener()方法的第二个参数十一哥函数,所以你不能在addEventListener()方法中为那个函数指定参数,你必须在监听器函数中定义那些额外的参数,并使用这些参数调用最终的方法。如果你在行内定义了一个事件监听器,你就可以添加监听器函数声明的任意数量的参数。下面的例子讲一个字符串和Event对象传递给了runMove方法:
<?xml version="1.0"?>
<!-- events/MultipleHandlerParametersInline.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script><![CDATA[
public function runMove(dir:String, e:Event):void {
if (dir == "up") {
moveableButton.y = moveableButton.y – 5;
} else if (dir == "down") {
moveableButton.y = moveableButton.y + 5;
} else if (dir == "left") {
moveableButton.x = moveableButton.x - 5;
} else if (dir == "right") {
moveableButton.x = moveableButton.x + 5;
}
}
]]></mx:Script>
<mx:Canvas height="100%" width="100%">
<mx:Button id="moveableButton"
label="{moveableButton.x.toString()},{moveableButton.y.toString()}"
x="75"
y="100"
width="80"
/>
</mx:Canvas>
<mx:VBox horizontalAlign="center">
<mx:Button id="b1"
label="Up"
click='runMove("up",event);'
width="75"
/>
<mx:HBox horizontalAlign="center">
<mx:Button id="b2"
label="Left"
click='runMove("left",event);'
width="75"
/>
<mx:Button id="b3"
label="Right"
click='runMove("right",event);'
width="75"
/>
</mx:HBox>
<mx:Button id="b4"
label="Down"
click='runMove("down",event);'
width="75"
/>
</mx:VBox>
</mx:Application> |
转载于:https://blog.51cto.com/flexria/154537