我的布局中有一个EditText和一个Button。

在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?


当前回答

这种方法总是不惜任何代价。只要在你想隐藏键盘的地方使用它

public static void hideSoftKeyboard(Context mContext,EditText username){
        if(((Activity) mContext).getCurrentFocus()!=null && ((Activity) mContext).getCurrentFocus() instanceof EditText){
            InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(username.getWindowToken(), 0);
        }
    }

这样使用:

无论Android版本是什么。这种方法肯定会奏效的

其他回答

对于Xamarin.Android:

public void HideKeyboard()
{
    var imm = activity.GetSystemService(Context.InputMethodService).JavaCast<InputMethodManager>();
    var view = activity.CurrentFocus ?? new View(activity);
    imm.HideSoftInputFromWindow(view.WindowToken, HideSoftInputFlags.None);
}

我花了两天多的时间研究了帖子中发布的所有解决方案,发现它们在某种程度上有所欠缺。我的确切要求是有一个按钮,可以100%可靠地显示或隐藏屏幕键盘。当键盘处于隐藏状态时,无论用户单击什么输入字段,都不应再次出现。当键盘处于可见状态时,不管用户单击什么按钮,都不应该消失。这需要在Android 2.2+上运行,直到最新的设备。

你可以在我的应用程序clean RPN中看到这一点的有效实现。

在许多不同的手机(包括froyo和姜饼设备)上测试了许多建议的答案后,很明显,android应用程序可以可靠地:

暂时隐藏键盘。当用户聚焦新的文本字段。活动开始时显示键盘并在活动上设置一个标志,指示他们的键盘应该始终可见。仅当活动为初始化。将活动标记为从不显示或允许使用键盘仅当活动为初始化。

对我来说,暂时隐藏键盘是不够的。在某些设备上,一旦新的文本字段被聚焦,它就会重新出现。当我的应用程序在一个页面上使用多个文本字段时,聚焦一个新的文本字段将导致隐藏的键盘再次弹出。

不幸的是,列表中的第2项和第3项仅在活动开始时工作可靠。一旦活动可见,就不能永久隐藏或显示键盘。诀窍是当用户按下键盘切换按钮时实际重新启动活动。在我的应用程序中,当用户按下切换键盘按钮时,会运行以下代码:

private void toggleKeyboard(){

    if(keypadPager.getVisibility() == View.VISIBLE){
        Intent i = new Intent(this, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        Bundle state = new Bundle();
        onSaveInstanceState(state);
        state.putBoolean(SHOW_KEYBOARD, true);
        i.putExtras(state);

        startActivity(i);
    }
    else{
        Intent i = new Intent(this, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        Bundle state = new Bundle();
        onSaveInstanceState(state);
        state.putBoolean(SHOW_KEYBOARD, false);
        i.putExtras(state);

        startActivity(i);
    }
}

这会导致当前活动将其状态保存到一个Bundle中,然后启动该活动,并传递一个布尔值,该布尔值指示键盘是显示还是隐藏。

在onCreate方法中,运行以下代码:

if(bundle.getBoolean(SHOW_KEYBOARD)){
    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(newEquationText,0);
    getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
else{
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
            WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}

如果应该显示软键盘,则会告知InputMethodManager显示键盘,并指示窗口使软输入始终可见。如果软键盘应隐藏,则设置WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM。

这种方法在我测试过的所有设备上都能可靠地工作,从运行android 2.2的4岁HTC手机到运行4.2.2的nexus 7。这种方法的唯一缺点是需要小心处理后退按钮。由于我的应用程序基本上只有一个屏幕(它是一个计算器),我可以覆盖onBackPressed()并返回设备主屏幕。

隐藏软键盘也很有用:

getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);

这可用于抑制软键盘,直到用户实际触摸editText View。

试试这个

简单,您可以在活动中调用


 public static void hideKeyboardwithoutPopulate(Activity activity) {
    InputMethodManager inputMethodManager =
            (InputMethodManager) activity.getSystemService(
                    Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(
            activity.getCurrentFocus().getWindowToken(), 0);
}

在您的主活动中调用


 hideKeyboardwithoutPopulate(MainActivity.this);

多亏了这个SO答案,我得出了以下结论,在我的情况下,当滚动浏览ViewPager的片段时,它很好地工作。。。

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