Hide keyboard when navigating from a fragment to another

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

提交回复
热议问题