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

den*_*nno 0 keyboard android customdialog android-edittext

我遇到了一个editText的情况.当我在编辑文本中按添加时,我在列表中添加成员.何时添加(或不添加)我正在打开自定义对话框.

在我的活动中,当点击编辑文本中的添加按钮时,我有这个代码:

  customDialogTeamMember = new CustomDialogTeamMember(............);
  customDialogTeamMember.makeDialog();
  editText.getText().clear();
  editText.clearFocus();
  hideSoftKeyboard();
Run Code Online (Sandbox Code Playgroud)

我的hideSoftKeyboard()定义如下:

public void hideSoftKeyboard() {
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
Run Code Online (Sandbox Code Playgroud)

此方法适用于应用程序的其他部分.但这里没有用!

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

小智 6

显示和隐藏键盘

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);
    }
}
Run Code Online (Sandbox Code Playgroud)