Alert preventing focusOut from actually changing the focus

1.3k views Asked by At

Here is a functional example:

<?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>

When you change from txtOne to txtTwo, the Alert is showed and after pressing OK, the focus will return to txtOne. I don't want that to happen. How to solve this?

2

There are 2 answers

1
moropus On BEST ANSWER

Alert.show has close callback argument, so you will know, when it is closed and set focus to something else. Update: you should have flag, indicating, that focus out event can be processed:

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;
}
0
Phill On

Try this also to ensure the focus goes where the user wanted it too

<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>