EventListener的add和remove

<?xml version="1.0" encoding="utf-8"?>
<mx:Application name="Button_willTrigger_buttonDown_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            import mx.utils.StringUtil;

            private function verify_click():void {
                var listener:Boolean = btn.hasEventListener(FlexEvent.BUTTON_DOWN);
                var trigger:Boolean = btn.willTrigger(FlexEvent.BUTTON_DOWN);
                var str:String = "hasEventListener() = {0}{1}willTrigger() = {2}";
                Alert.show(StringUtil.substitute(str, listener, "n", trigger));
            }

            private function addEventListener_click():void {
                btn.addEventListener(FlexEvent.BUTTON_DOWN, btn_buttonDown);
                verify_click();
            }

            private function removeEventListener_click():void {
                btn.removeEventListener(FlexEvent.BUTTON_DOWN, btn_buttonDown);
                verify_click();
            }

            private function btn_buttonDown(evt:FlexEvent):void {
                Alert.show(evt.type);
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Verify listeners"
                click="verify_click();" />

        <mx:Spacer width="100%" />

        <mx:Button label="addEventListener()"
                click="addEventListener_click();" />
        <mx:Button label="removeEventListener()"
                click="removeEventListener_click();" />
    </mx:ApplicationControlBar>

    <mx:Button id="btn" label="Button" />

</mx:Application>
 

你可能感兴趣的:(xml,Adobe)