<?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="1024" minHeight="768" pageTitle="TheStudioOfCenyebao">
<!-- Demo_添加、移除事件监听器示例 -->
<fx:Declarations>
</fx:Declarations>
<fx:Script>
<![CDATA[
/**
* 添加、移除监听器;
*/
protected function toggleListeners():void{
if(rectBox.hasEventListener(MouseEvent.CLICK)) {
log("=>移除事件监听器");
rectBox.removeEventListener(MouseEvent.MOUSE_OVER, onEvent);
rectBox.removeEventListener(MouseEvent.MOUSE_OUT, onEvent);
rectBox.removeEventListener(MouseEvent.MOUSE_MOVE, onEvent);
rectBox.removeEventListener(MouseEvent.CLICK, onEvent);
}else {
log("=>添加事件监听器");
rectBox.addEventListener(MouseEvent.MOUSE_OVER, onEvent);
rectBox.addEventListener(MouseEvent.MOUSE_OUT, onEvent);
rectBox.addEventListener(MouseEvent.MOUSE_MOVE, onEvent);
rectBox.addEventListener(MouseEvent.CLICK, onEvent);
}
}
/**
* 打印日志;
*/
protected function log(logStr:String):void{
outfield.text = logStr + "\n" + outfield.text;
}
/**
* 事件触发的函数;
*/
protected function onEvent(event:Event):void{
log("事件类型:" + event.type);
}
]]>
</fx:Script>
<!--view-->
<s:VGroup verticalCenter="0" horizontalCenter="0" width="300" height="350">
<s:HGroup width="100%" height="100" verticalAlign="bottom">
<s:Group id="rectBox" width="175" height="100%">
<s:Rect width="100%" height="100%">
<s:fill>
<s:SolidColor color="0x979797"/>
</s:fill>
</s:Rect>
</s:Group>
<s:Button id="toggleBtn" label="ToggleListeners" click="toggleListeners()"/>
</s:HGroup>
<s:TextArea id="outfield" width="100%" height="100%"/>
</s:VGroup>
</s:Application>
<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="1024" minHeight="768" pageTitle="TheStudioOfCenyebao">
<!-- Demo_添加、移除事件监听器示例 -->
<fx:Declarations>
</fx:Declarations>
<fx:Script>
<![CDATA[
/**
* 添加、移除监听器;
*/
protected function toggleListeners():void{
if(rectBox.hasEventListener(MouseEvent.CLICK)) {
log("=>移除事件监听器");
rectBox.removeEventListener(MouseEvent.MOUSE_OVER, onEvent);
rectBox.removeEventListener(MouseEvent.MOUSE_OUT, onEvent);
rectBox.removeEventListener(MouseEvent.MOUSE_MOVE, onEvent);
rectBox.removeEventListener(MouseEvent.CLICK, onEvent);
}else {
log("=>添加事件监听器");
rectBox.addEventListener(MouseEvent.MOUSE_OVER, onEvent);
rectBox.addEventListener(MouseEvent.MOUSE_OUT, onEvent);
rectBox.addEventListener(MouseEvent.MOUSE_MOVE, onEvent);
rectBox.addEventListener(MouseEvent.CLICK, onEvent);
}
}
/**
* 打印日志;
*/
protected function log(logStr:String):void{
outfield.text = logStr + "\n" + outfield.text;
}
/**
* 事件触发的函数;
*/
protected function onEvent(event:Event):void{
log("事件类型:" + event.type);
}
]]>
</fx:Script>
<!--view-->
<s:VGroup verticalCenter="0" horizontalCenter="0" width="300" height="350">
<s:HGroup width="100%" height="100" verticalAlign="bottom">
<s:Group id="rectBox" width="175" height="100%">
<s:Rect width="100%" height="100%">
<s:fill>
<s:SolidColor color="0x979797"/>
</s:fill>
</s:Rect>
</s:Group>
<s:Button id="toggleBtn" label="ToggleListeners" click="toggleListeners()"/>
</s:HGroup>
<s:TextArea id="outfield" width="100%" height="100%"/>
</s:VGroup>
</s:Application>