Hide keyboard when navigating from a fragment to another

后端 未结 5 1248
我寻月下人不归
我寻月下人不归 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:06

    Put the code that hides the keyboard in your "save button" click listener, and use this method to hide the keyboard:

        public static 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);
             }
         }
    
    0 讨论(0)
  • 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);
             }
         }
    
    0 讨论(0)
  • 2021-01-30 17:11
    @Override
        public void onDestroyView() {
            super.onDestroyView();
            View view = getActivity().getCurrentFocus();
            if (view != null) {
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
    
    0 讨论(0)
  • 2021-01-30 17:17

    Kotlin

    For Kotlin, you can use this as a top level function, just add the code to a separate class such as Utils.kt.

    fun hideKeyboard(activity: Activity) {
        val inputMethodManager =
            activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    
        // Check if no view has focus
        val currentFocusedView = activity.currentFocus
        currentFocusedView?.let {
            inputMethodManager.hideSoftInputFromWindow(
                currentFocusedView.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
        }
    }
    

    To access it from Fragment, call it like:

    hideKeyboard(activity as YourActivity)
    

    Thanks to Silvia H for Java code.

    0 讨论(0)
  • 2021-01-30 17:17

    Easiest way to hide keyboard in fragment or Activity

    Soluton : 1

    //hide keyboard
    public static void hideKeyboard(Context ctx) {
        InputMethodManager inputManager = (InputMethodManager) ctx
                .getSystemService(Context.INPUT_METHOD_SERVICE);
    
        // check if no view has focus:
        View v = ((Activity) ctx).getCurrentFocus();
        if (v == null)
            return;
    
        inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
    

    Solution : 2

        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    
    0 讨论(0)
提交回复
热议问题