在关闭自定义对话框后在 Android 中隐藏软键盘

Hide soft Keyboard in Android after a custom dialog dismiss

我现在有一个 editText。当我在编辑文本中按添加时,我在列表中添加了一个成员。何时添加(或不添加)我正在打开自定义对话框。

在我的 activity 中,当我点击编辑文本中的添加按钮时,我得到了这段代码:

  customDialogTeamMember = new CustomDialogTeamMember(............);
  customDialogTeamMember.makeDialog();
  editText.getText().clear();
  editText.clearFocus();
  hideSoftKeyboard();

我的 hideSoftKeyboard() 定义如下:

public void hideSoftKeyboard() {
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

此方法适用于应用程序的其他部分。但是这里不行!

自定义对话框打开。当我关闭它时,键盘一直在屏幕上。可能是什么问题?!

我在我的案例中使用了这个。

// for hide keyboard
    public static void hideKeyboard(Activity activity) {
        InputMethodManager iim = (InputMethodManager)
                activity.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (activity.getCurrentFocus() != null)
            iim.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
    }

将此方法作为您的 requirements.and 告诉我..希望这会有所帮助。

显示和隐藏键盘

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
//Hide:
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
//Show
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);




private void hideKeyboard() {   
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

private void showKeyboard() {   
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
}
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {  
     InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
     imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

看看这个

要隐藏和显示软键盘,使用以下方法

private void hideKeyboard() {   
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

private void showKeyboard() {   
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
}
// minimize keyboard
    try {
        InputMethodManager imm = (InputMethodManager)activity.getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(dialog.getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {}