Android 如何在用户点击对话框外部时隐藏软键盘

Android 如何在用户点击对话框外部时隐藏软键盘,android,dialog,keyboard,Android,Dialog,Keyboard,如果用户点击的不是软键盘或编辑文本上的空格,对话框将关闭,在这种情况下我如何关闭键盘?使用此选项 View view = this.getCurrentFocus(); if (view != null) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindow

如果用户点击的不是软键盘或编辑文本上的空格,对话框将关闭,在这种情况下我如何关闭键盘?

使用此选项

View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

尝试此方法隐藏键盘:

 public void hideKeyboard(Activity activity) {
        try {
            InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (inputManager!=null) {
                if (activity.getCurrentFocus() == null)
                    return;
                if (activity.getCurrentFocus().getWindowToken() == null)
                    return;
                inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
            }
        } catch (Exception hideKeyboardException) {
            // handle exception here
        }
    }

如果您没有任何活动,请从此方法中删除活动对象。

我不确定,是否有帮助

    public void hideSoftKeyboard() {
    View view = this.getCurrentFocus();
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}