How to hide the keyboard the moment the user presses a button?

1

I would like my application to force the cellphone to close whenever the user presses a button (above and next to the edit).

    
asked by anonymous 18.10.2017 / 17:48

2 answers

1

Within the click of the button, in the onClickListener, put this code:

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

This will cause the device's virtual keyboard to close.

    
18.10.2017 / 18:18
0

Call this method:

/**
 * Esconda o teclado
 */
public void hideSoftKeyboard() {
    if(getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

/**
 * Mostre o teclado
 */
public void showSoftKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    view.requestFocus();
    inputMethodManager.showSoftInput(view, 0);
}

Based on this excellent SOen response: link

    
19.02.2018 / 01:26