警报阻止focusOut实际更改焦点

时间:2011-10-26 13:03:57

标签: flex focus focusout

这是一个功能性的例子:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            public function go():void{              
                Alert.show("The focus will return to txtOne. Look: ");
            }
        ]]>
    </mx:Script>
    <mx:VBox>
        <mx:TextInput id="txtOne" text="1" focusOut="go()"/>
        <mx:TextInput id="txtTwo" text="2"/>    
    </mx:VBox>  
</mx:Application>

当您从txtOne更改为txtTwo时,会显示警报,按OK后,焦点将返回到txtOne。我不希望这种情况发生。怎么解决这个问题?

2 个答案:

答案 0 :(得分:0)

Alert.show具有关闭回调参数,因此您将知道,当它关闭并将焦点设置为其他内容时。 更新:您应该有一个标志,表示可以处理焦点输出事件:

private var needAlert:Boolean = true;

public function go():void
{
    if (needAlert)
    {
        Alert.show("The focus will return to txtOne. Look: ",
            "", 0, null, myCloseHandler);
        needAlert = false;
    }
}

private function myCloseHandler(event:CloseEvent):void
{
    this.setFocus();
    needAlert = true;
}

答案 1 :(得分:0)

尝试这一点也可以确保重点转移到用户想要的地方

<mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.events.CloseEvent;
        import mx.managers.IFocusManagerComponent;
        private var ignoreFocusChange:Boolean = false;

        private var focusTargetOnReturn: IFocusManagerComponent;

        public function go():void
        {

            if(!ignoreFocusChange){
                focusTargetOnReturn = focusManager.getFocus();

                Alert.show("The focus will not return to txtOne anymore. Look: ",
                    "", 0, null, myCloseHandler);
            }
        }

        private function myCloseHandler(event:CloseEvent):void
        {               
            ignoreFocusChange = true;
            focusManager.setFocus(focusTargetOnReturn);
            ignoreFocusChange = false;
        }
    ]]>
</fx:Script>
<mx:VBox>
    <mx:TextInput id="txtOne" text="1" focusOut="go()"/>
    <mx:TextInput id="txtTwo" text="2"/>    
</mx:VBox>