当使用mx.controls.Alert.show()显示警报框并使用鼠标(或 Enter 键)单击它的按钮或单击任意位置时,这些事件也会传递到父窗口,最终触发那里的按钮等。
有没有直接的方法来防止这种情况,比如设置警报实例的属性?
当使用mx.controls.Alert.show()显示警报框并使用鼠标(或 Enter 键)单击它的按钮或单击任意位置时,这些事件也会传递到父窗口,最终触发那里的按钮等。
有没有直接的方法来防止这种情况,比如设置警报实例的属性?
如果在show()closeHandler
中设置参数,则可以调用event.stopPropagation()以防止在后续节点上处理事件。
代码看起来像这样:
Alert.show("Message", "Title", Alert.OK, parent, onClose);
function onClose(event:CloseEvent):void
{
event.stopPropagation();
// Do close stuff here
}
在 Flash Builder 4.6 中添加了 Flex 4.6 的示例:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="onCreationComplete(event)">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
private function onCreationComplete(e:FlexEvent):void
{
addEventListener(MouseEvent.CLICK, onAppClick);
showAlert();
}
private function showAlert():void
{
Alert.show("An Alert", "Alert Title", Alert.OK | Alert.CANCEL, this, onAlertClose);
}
private function onAlertClose(e:CloseEvent):void
{
e.stopPropagation();
trace("Alert Close");
}
private function onAppClick(e:MouseEvent):void
{
trace("App Click");
}
private function onBtnClick(e:MouseEvent):void
{
trace("Button Click");
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout
gap="12"/>
</s:layout>
<s:Button
label="A Clickable Button"
click="onBtnClick(event)"/>
<s:Button
label="Show Alert"
click="showAlert()"/>
</s:WindowedApplication>