Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep soft keyboard open when enter key is pressed

Well, I'm trying to prevent the soft keyboard from closing when the user press the "ok" button after editing a text field. Actually, what i'm trying to achieve is : when "ok" button is pressed, the field is parsed and if valid, it starts another activity. That is easy enough.

But, when the field is not valid, I want the soft keyboard to stay open. And that's... a hell more complicated. If anyone know how to achieve such a thing...

Thanks in advance.

EDIT : what I mean by the OK button is the OK button from the soft keyboard.

like image 540
Redwarp Avatar asked Aug 26 '11 09:08

Redwarp


People also ask

How do I enable soft keyboard on Android?

To enable your latest Android keyboard, scroll to the bottom and hit the System entry. Then, click Languages & input. Pick Virtual keyboard on the following page. You will find a list of all the existing keyboards on your smartphone here.


1 Answers

Attach OnEditorActionListener to your text field and return true from its onEditorAction method, when actionId is equal to IME_ACTION_DONE. This will prevent soft keyboard from hiding:

EditText txtEdit = (EditText) findViewById(R.id.txtEdit);
txtEdit.setOnEditorActionListener(new OnEditorActionListener() {

  public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE) {
      // your additional processing... 
      return true;
    } else {
      return false;
    }
  }

});
like image 88
Idolon Avatar answered Nov 03 '22 19:11

Idolon