Force keyboard opening

2

I have this dialog in my application

public void alertaLocalizar(View v) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.alerta_comum, null);


    Dialog localizar = new Dialog(this);
    localizar.requestWindowFeature(Window.FEATURE_NO_TITLE);
    localizar.setContentView(view);
    localizar.show();
    }

}

And I wanted to force the keyboard to open as soon as it was opened, how can I do that?

And taking advantage of the question, I would like to know how to use the OK button on the keyboard to have the same effect as the OK button on the alert and not close the keyboard.

    
asked by anonymous 13.06.2016 / 21:07

1 answer

3

To force the keyboard to open use this code:

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

To hide the keyboard, place this code inside the button:

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

Take a look at the InputMethodManager documentation for better understanding:

link

And complementing with your question about where to put the code, I suggest you create an event to handle the OK button on the keyboard to hide the keyboard through the code I posted.

Example of the Enter button with KeyEvent event:

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_ENTER:
          /*Coloque aqui o que irá acontecer caso pressionado o botão*/
          return true;
        }
    return false;
}

Take a look at the KeyEvent event documentation that will help you do this:

link

    
13.06.2016 / 21:32