Hide soft keyboard after dialog dismiss

前端 未结 8 1853
孤街浪徒
孤街浪徒 2021-01-31 02:16

I want to hide soft keyboard after AlertDialog dismiss, but it\'s still visible. Here is my code:

alert = new AlertDialo         


        
8条回答
  •  别跟我提以往
    2021-01-31 02:55

    protected void hideKeyboard() {
        final Activity activity = getActivity();
        final View view = activity != null ? activity.getCurrentFocus() : null;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (view != null) {
                    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null)
                        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
            }
        }, 1);
    }
    
    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        hideKeyboard();
    }
    

提交回复
热议问题