警报和鼠标&关键事件

时间:2013-01-16 15:57:40

标签: actionscript-3 air actionscript-2

当显示带有 mx.controls.Alert.show()的警告框并用鼠标(或Enter键)单击其按钮或单击任意位置时,这些事件将作为父窗口传递给好吧,最终在那里触发按钮等。

是否有直接的方法来阻止这种情况,例如设置警报实例的属性?

1 个答案:

答案 0 :(得分:0)

如果您在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>