How to hide keyboard on dialog dismiss

梦想的初衷 提交于 2019-12-10 03:41:55

问题


I have an Activity with single Fragment on it. There is one EditText on the fragment.

The keyboard is popping up as soon the fragment is shown, however I managed to block it setting in the manifest android:windowSoftInputMode="stateHidden"

However, there also is a button, which opens a dialog with another EditText.

I have a method that automatically closes the keyboard on dialog dismiss.

public static void closeInput(final View caller) {      
    caller.post(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) caller.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(caller.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
        }
    });
}

The method is not a pretty hack and there is something wrong about it. Dialog's EditText has inputType="numberDecimal". The closeInput() seems to be not closing the keyboard, only changing it to the default alphabetical state.

What is going on here?


回答1:


In my case I used method:

public static void closeInput(final View caller) {  
    caller.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) caller.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(caller.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }, 100);
}

It was refusing to work properly because of activity settings in Manifest, if I recall you can't have android:windowSoftInputMode="any_of_these" set




回答2:


From fragments onCreateView() method you can do this:

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

It will automatically hide soft keyboard on Dismiss of Dialog




回答3:


in BaseDialog.java

protected void hideSoftKeyboard() {
    InputMethodManager imm = (InputMethodManager) this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm.isActive()) {
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
}



回答4:


In my case, solution was to put keyboard hide in dialog dismiss

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            View view = activity.getCurrentFocus();
            if (view != null) {
                InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }); 



回答5:


Struggling with this issue and after reviewing the answers here, most seem to actually work. Since it was not desired to use a class rather just a builder, the answer https://stackoverflow.com/a/36422411/1815624 is not a workable solution.

Realizing others may have this same issue, an answer is being derived from both: https://stackoverflow.com/a/17393446/1815624 & https://stackoverflow.com/a/32648971/1815624

So the combination answer is grabbing the view from the fragment itself:

(Anyone got a reason not to?)

void closeKeyboard(final View caller){
    caller.post(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) caller.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(caller.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    });
}

...

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        closeKeyboard(getView());
    }
});


来源:https://stackoverflow.com/questions/16106486/how-to-hide-keyboard-on-dialog-dismiss

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