How to press "Next" on the last EditText on the screen and return to the first EditText

StackOverflow https://stackoverflow.com/questions/19987082

  •  30-07-2022
  •  | 
  •  

Вопрос

I haven't seen this before and I just figured it out, so I thought I'd write this QnA. Let me know if you think this is a duplicate.

Question:

How can I make it so that when the user is focused on the last EditText on the screen, it will give them the option on the soft keyboard to press Next and then will return to the first EditText on the screen?

For example:

enter image description here

Notice in the bottom right corner is says "Next". I want it to say "Next" like that instead of "Done" and I also want it to return to (focus on) the a EditText when I press "Next", currently focused on the c EditText.

Это было полезно?

Решение

In your EditText XML properties for EditText c, add this property to it:

android:imeOptions="actionNext"

This will make it so the bottom right corner of the soft keyboard will be the "Next" button instead of "Done".

Then, set up a listener for when the "Next" button is pressed on the EditText c.

EditText a = (EditText)findViewById(R.id.a);
EditText c = (EditText)findViewById(R.id.c);

c.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
    {
        // if the user pressed the "Next" button on the soft keyboard
        if(actionId==EditorInfo.IME_ACTION_NEXT)
        {
            a.requestFocus(); // change the focus to the 'a' EditText
        }
        return true; // keep the keyboard up
    }
});

And that's it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top