Hide Soft keyboard on return key press

前端 未结 9 1246
[愿得一人]
[愿得一人] 2020-12-29 19:00

I\'ve searched half a dozen other answers on SO, but haven\'t found one that works. All I\'m trying to do is dismiss the soft keyboard when the user presses the enter butto

9条回答
  •  粉色の甜心
    2020-12-29 19:26

    Try this method, It may be solve your problem.

    protected void showKeyboard() {
    
                InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (activity.getCurrentFocus() == null) {
                    inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
                } else {
                    View view = activity.getCurrentFocus();
                    inputMethodManager.showSoftInput(view,InputMethodManager.SHOW_FORCED);
                }
            }
    
            /**
             * Hide keyboard.
             */
            protected void hideKeyboard() {
    
                InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                View view = activity.getCurrentFocus();
                if (view == null) {
                    if (inputMethodManager.isAcceptingText())
                        inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
                } else {
                    if (view instanceof EditText)
                        ((EditText) view).setText(((EditText) view).getText().toString()); // reset edit text bug on some keyboards bug
                    inputMethodManager.hideSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }          
            }
    

提交回复
热议问题