我正在尝试扩展 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 选项的消息警报,并防止我不得不在触发服务器的每个按钮上编写它。不幸的是,它不是那样工作的。
- 尝试删除默认功能并添加我在侦听器上编写的功能;
- 尝试直接覆盖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));
}
}
}
}