Open Keyboard for another EditView when click Enter

59 views Asked by At

I would like to open the keyboard for another EditView (than the one I am using) when I click Enter on it (when I am in the first EditView).

The scenario should be like this : I am writing something in the keyboard for the 1st EditView, I have finished so I press "Enter" and it opens the keyboard for the second EditView.

I tried different codes but no success, if somebody could help me please.

3

There are 3 answers

0
user3460225 On BEST ANSWER

I have found by searching with 'setNextFocusDownld' like Kasper suggested, using this answer : Android Softkey's next button not taking focus to spinner

The code I used is:

mEditText.setNextFocusDownId(R.id.textView2b);
        mEditText.setOnKeyListener(new OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on Enter key press
                    mEditText.clearFocus();
                    mEditText2.requestFocus();
                    return true;
                }
                return false;
            }
        });
        mEditText2.setOnKeyListener(new OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on Enter key press
                    mEditText2.clearFocus();
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(mEditText2.getWindowToken(), 0);
                    return true;
                }
                return false;
            }
        });
2
Igor Ronner On

if i understood here is the answer

editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
2
Moonbloom On

You basically want to switch the focus from 1 EditText to another after pressing Enter.

This can very easily be done by setting the 'setNextFocusDownId' option on your 1st EditText.

firstEditText.setNextFocusDownId(R.id.secondEditTextId);