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

时间:2017-04-10 09:08:39

标签: android android-edittext keyboard customdialog

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

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

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

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

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

5 个答案:

答案 0 :(得分:5)

显示和隐藏键盘

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

答案 1 :(得分:0)

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

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

将此方法称为您的要求。让我知道。希望这会有所帮助。

答案 2 :(得分:0)

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

检查出来

答案 3 :(得分: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);
    }
}

答案 4 :(得分:0)

// minimize keyboard
    try {
        InputMethodManager imm = (InputMethodManager)activity.getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(dialog.getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {}
相关问题