user2978042
user2978042

Reputation: 221

Prevent EditText to gain focus on Next button click of soft keyboard

I have a fragment view and one fragment have some sets of EditTexts and the another has one EditText as well. The problem I have is, I click Next button in softkeyboard when it is in a EditText, it goes to the next EditText in the same fragment view. But after the third EditText, on Next in softkeyboard, it goes to the Edittext which is in the next fragment view. Vertically, the EditText is just below the 3rd EditText. Is it because of that ?

Upvotes: 2

Views: 910

Answers (2)

kokoko
kokoko

Reputation: 3982

you can use following code:

youredittext.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(keyCode == event.KEYCODE_ENTER)
            {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        prepareMonthDialog();
                    }
                }, 500);
               return true;
            }

            return false;
        }
    });

Upvotes: 0

asok Buzz
asok Buzz

Reputation: 1894

yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_NEXT) {
                    // do something with next printed on your editText

                }
                return true;
            }
        });

The key factor is you return true which tells that you have consumed the Next action, so no more focus by default.

Returns
Return true if you have consumed the action, else false. 

Upvotes: 2

Related Questions