Search code examples
iosactionscript-3airfocustextfield

AS3/Air Shift focus from one text field to the next in ios


I am using Flash Pro cs6, AS3, Air3.8.. And I am using textfield for input. I am trying to make it so when the user presses "Done" it shifts to the next field. I am finding conflicting information about this on all the forums, including this one. I did search but never found a fix.. I am definitely receiving the events, and I tried adding a line that identifies the keycode, which has confirmed it is receiving keycode 13. I actually made it put the keycode into the field I want the focus to shift to successfully.. It just refuses to put focus on that field..

The code I am using is stated in the docs that it will not work in iOS.. BUT it DOES work further along on the same page of script, as well as on two other ones... I have:

stage.addEventListener(KeyboardEvent.KEY_DOWN,keyhandler);


function keyhandler(event:KeyboardEvent){
if(event.charCode == 13)
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyhandler);
stage.focus = null;
stage.focus = nextTextField;
}
}

I tried that while adding the listener to the text field first, then changed it to the stage, neither work.. I tried "requestsSoftKeyboard" and several other ways I have seen posted that supposedly work in iOS, but they didn't work.. There are other functions that use the same stage.focus to null, followed by stage.focus to MyTextField and they work, but they are initiated by pressing a button on the stage, NOT a button on the soft keyboard. The code there that works is just a standard if statement:

if (TextField == "")
{
stage.focus=null;
stage.focus=TextField;
}else if (NextTextField == "")
{
stage.focus = null;
stage.focus = NextTextField;
}else if (TextFieldAfterThat == "")
{
stage.focus = null;
stage.focus = TextFieldAfterThat;
}

That continues through all fields and always goes to the right one with the soft keyboard open, cursor blinking, and ready to type.. every time. I know the listeners are received from the soft keyboard "Done" because a function to capitalize the words works, and when I added code to confirm the keycode it worked. I also have found access to that value using "charcode" AND "keycode".. I do not know what the difference is, but both returned 13 and neither worked for me.. There is another place I use the same code to make a TextField active and set the focus after the user presses a radio button, and those all work every time.

I am not sure what the difference is coming from pressing "Done" vs. pressing an object on the stage, but it refuses to set the focus with the done button. Anyone have any ideas or made this work before?


Solution

  • I had success assingning focus to a StageText in iOS like this:

    stageText.assignFocus();
    

    StageText offers many advantages over TextField because it shows a native text input. The only disadvantage I know is that you can't use custom fonts.

    Here's the documentation, and a tutorial.

    I haven't experimented with the "Done" key, but I did what you are trying to achieve with "Enter" key and it worked. Also take note that in iOS the "Done" key is meant to hide the keyboard, so that could be why you are having these problems..