Hide soft keyboard after dialog dismiss

前端 未结 8 1715
孤街浪徒
孤街浪徒 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 03:01

    I had a similar problem when closing an alert dialog. This seems to do the trick for me.

    Inside your DialogFragment

    public static void closeKB(final View view) 
    {
        caller.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 1);
    }
    
    @Override
    public void onDismiss(DialogInterface dialog)
    {
        super.onDismiss(dialog);
                View view = getActivity().getCurrentFocus();
        if (view != null)
        {
            closeKB(view);
        }
    }
    

提交回复
热议问题