Hide soft keyboard after dialog dismiss

前端 未结 8 1698
孤街浪徒
孤街浪徒 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:42

    All these advices to use InputMethodManager are somewhat vague - where exactly to call it,
    and they do not work at least for me.
    Yes, keyboard disappears but then the app crashes!?
    The main problem is that hiding of keyboard happens at the same time when dialog is disappearing.

    To avoid it dialog.dismiss() should be called in view.postDelayed() after imm.hideSoftInputFromWindow() and in my case I set delay as 150.

    0 讨论(0)
  • 2021-01-31 02:44

    I use this method:

    IBinder token = searchTextEntry.getWindowToken();
    ( ( InputMethodManager ) getSystemService( Context.INPUT_METHOD_SERVICE ) ).hideSoftInputFromWindow( token, 0 );
    

    Where searchTextEntry is the name of my EditText reference.

    0 讨论(0)
  • 2021-01-31 02:45

    in case anyone looks for this in kotlin, it would be:

    private fun hideDeviceKeyboard() {
        val imm = context!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
    }
    
    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
  • 2021-01-31 02:57

    In Manifest xml

    android:windowSoftInputMode="stateAlwaysHidden"
    

    It will automatically hide soft keyboard on Dismiss of Dialog

    0 讨论(0)
  • 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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题