1

我正在尝试扩展 Button 类并通过以下方式删除默认 EventListener:

removeEventListener(MouseEvent.CLICK, clickHandler);

然后添加如下内容:

    protected function _clickHandler(event:MouseEvent):void{
        Alert.show("Are you sure about this operation", "Alert", 3, this, execute);
        function execute(e:CloseEvent):void{
            if(e.detail == Alert.YES)
                super.clickHandler(event);
        }
    } 

这样,我将拥有一个默认组件,该组件将触发带有 YES 或 NO 选项的消息警报,并防止我不得不在触发服务器的每个按钮上编写它。不幸的是,它不是那样工作的。

  1. 尝试删除默认功能并添加我在侦听器上编写的功能;
  2. 尝试直接覆盖clickHandler,也不行;

编辑:这就是我想要的:当用户单击将在我的应用程序中进行服务调用的按钮时,我总是会弹出一个窗口让他告诉我他是否确定。我想要的是为此构建一个自动组件,如下所示:

package screen{
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;

import mx.controls.Alert;
import mx.controls.Button;
import mx.events.CloseEvent;

public class CommonButton extends Button{
    public function CommonButton(){
        super();
        //removeEventListener(MouseEvent.CLICK, clickHandler)
        //addEventListener(MouseEvent.CLICK, clickHandler);
        addEventListener(KeyboardEvent.KEY_DOWN, function (e:KeyboardEvent):void{
            if(e.altKey == Keyboard.ENTER)
                dispatchEvent(new MouseEvent(MouseEvent.CLICK));
        });
    }

    private var _saveEvent:MouseEvent;
    override protected function clickHandler(event:MouseEvent):void{
        _saveEvent = event;
        event.stopImmediatePropagation();
        Alert.show("Are you sure about this operation", "Alert", 3, this, execute);         
    } 

    private function execute(e:CloseEvent):void{
        if(e.detail == Alert.YES)
            super.clickHandler(_saveEvent);
    }
}
}

进而:

    <mx:Script>
    <![CDATA[
        import mx.controls.Alert;
        private function test():void{
            //if the user clicked No, this method will never be called.
            Alert.show("You clicked YES");
        }
    ]]>
</mx:Script>
<screen:CommonButton click="test()" /> 

最终编辑解决方案:

package screen{
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;

import mx.controls.Alert;
import mx.controls.Button;
import mx.events.CloseEvent;

public class CommonButton extends Button{
    public function CommonButton(){
        super();
        addEventListener(KeyboardEvent.KEY_DOWN, function (e:KeyboardEvent):void{
            if(e.altKey == Keyboard.ENTER)
                dispatchEvent(new MouseEvent(MouseEvent.CLICK));
        });
    }

    private var _stopProp:Boolean = true;
    override protected function clickHandler(event:MouseEvent):void{
        if(_stopProp){
            event.stopImmediatePropagation()
            Alert.show("Are you sure about this operation", "Alert", 3, this, execute);
        }else
            _stopProp = true;

    } 

    private function execute(e:CloseEvent):void{
        if(e.detail == Alert.YES){
            _stopProp = false;
            dispatchEvent(new MouseEvent(MouseEvent.CLICK));
        }
    }
}

}

4

2 回答 2

2

为什么还要为此烦恼?只需将您的自定义事件处理程序添加到扩展组件并允许正常事件处理程序触发即可。您可以将多个处理程序注册到同一个事件。我认为这是一个问题的唯一原因是,如果您将其作为库发布并希望阻止用户自己附加点击处理程序,但您不是。您根本不需要在此处删除或停止默认的单击处理程序。

编辑:这是提问者想要在这里发生的事情的一个例子。

public class AButton extends Button
{
    private var stopProp:Boolean = true;

    public function AButton()
    {
        super();
        this.addEventListener(MouseEvent.CLICK,this.clickHandlers);
    }

    private function clickHandlers( e:MouseEvent ):void{
        if ( stopProp ) {
            e.stopImmediatePropagation();

            trace( "clicked in button" );
            Alert.show( "Continue with event", "Warning!", Alert.YES|Alert.NO, null, this.closeHandler  );
        }
    }

    private function closeHandler( e:CloseEvent ):void {
        if ( e.detail == Alert.YES ) {
            this.stopProp = false;
            this.dispatchEvent( new MouseEvent( MouseEvent.CLICK ) );
            this.stopProp = true;
        }
    }
}

stopProp如果属性为真,这将阻止事件继续进行。然后它会提示用户。如果用户选择“是”,它会将 stopProp 设置为 false,从按钮重新调度 click 事件,并将 stopProp 重置为 false 以供下次使用。如果他们选择“否”,则不会发生任何事情。

我做了一个快速原型来测试它,它确实有效。

于 2013-01-25T16:05:12.830 回答
0

使用 Event 类的 stopImmediatePropagation() 方法。

protected function _clickHandler(event:MouseEvent):void{
    event.stopImmediatePropagation();

    Alert.show("Are you sure about this operation", "Alert", 3, this, execute);
    function execute(e:CloseEvent):void{
        if(e.detail == Alert.YES)
            super.clickHandler(event);
    }
}
于 2013-01-25T14:28:37.367 回答