Hide soft keyboard after dialog dismiss

自古美人都是妖i 提交于 2019-11-28 17:47:59
Rajeshwar

In Manifest xml

android:windowSoftInputMode="stateAlwaysHidden"

It will automatically hide soft keyboard on Dismiss of Dialog

I met the same problem. Solved it by doing like this. It doesn't need any reference:

imm.hideSoftInputFromWindow(getWindow().getDecorView()
                .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

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);
    }
}

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.

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();
}

I had trouble with all of these solutions, but if you are working with a Fragment, per @amal-dev-s-i in this thread: How to hide keyboard on dialog dismiss

I simply added this to my fragment and it worked after calling dismiss() on the dialog:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)

And I've had similar luck adding the above (removing getActivity()) to onResume() of the parent activity if it's within an activity.

user3130847

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.

This works! This will close the keyboard after dialog dismiss

InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!