Some days back, I got a mail from a customer with a baffling problem. Consider this…
- You have 3 TextInputs, adjacent in tab order in an application.
- You type something on the first, press tab, again type something on the next and tab again
- Now for some reason, you want the second tab to switch focus onto the first, instead of the last TextInput…
He wrote a listener for the focusOut event on TextInput2 and then wrote the following code in the listener function.
event.preventDefault();
stage.focus = txt1;
txt1.setSelection(txt1.text.length,txt1.text.length);
He expected this…
But got this…
After lots of digging, I found the solution, thanks to Sreeni 🙂
It seems that the problem is that though the application focus moves to TextInput1, the keyboard focus moves to TextInput3, as the keyboard foucs is moved before the focusOut method is called and we are able to call preventDefault() on it.
Sreeni dug out a KEY_FOCUS_CHANGE event which needs to be listened to, on TextInput2 and then repeated the above code for the event-listener method. So,
Instead of writing
<mx:TextInput id=”txt2″ focusOut=”setTxtFocus(event)”/>
You need to write the following code in the creationComplete Listener in AS
txt2.addEventListener(FocusEvent.KEY_FOCUS_CHANGE, setTxtFocus);
Now you can repeat the same code for the setTxtFocus method and solve this issue… You can see the app and the solution code below