flex_添加、移除事件監聽器示例;


<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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章