【发布时间】:2013-01-09 11:15:12
【问题描述】:
我正在尝试扩展 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);
}
}
这样,我将拥有一个默认组件,该组件将触发带有“是”或“否”选项的消息警报,并防止我不得不在触发服务器的每个按钮上编写该组件。不幸的是,它不是那样工作的。
- 尝试删除默认函数,并添加我写在监听器上的那个;
- 试过直接覆盖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));
}
}
}
}
【问题讨论】:
-
您能否发布显示您尝试覆盖 clickHandler 的代码?
-
你指的是 flash.display.SimpleButton 吗?你试图用你的覆盖来改变它的什么功能。您是否尝试先通过对话框拦截其他订阅的事件处理程序?
-
我希望在确认之前不允许调用 click 方法。基本上,我想将 click 方法存储在类中并强制要求确认,如果用户单击“是”,我会立即进行原始调用。我不想使用不同的属性来做到这一点,我实际上想继续使用“点击”。
标签: actionscript-3 event-handling