Hide keyboard when navigating from a fragment to another

后端 未结 5 1247
我寻月下人不归
我寻月下人不归 2021-01-30 16:17

I have a Fragment that contains an Edit Text. When the Edit Text is pressed, the keyboard is being shown. When pressed the Save button in the upper corner, the application retur

5条回答
  •  礼貌的吻别
    2021-01-30 17:10

      public void hideKeyboard(Activity activity) {
            InputMethodManager inputManager = (InputMethodManager) activity
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    
            // check if no view has focus:
             View currentFocusedView = activity.getCurrentFocus();
             if (currentFocusedView != null) {
                 inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
             }
         }
    

提交回复
热议问题