如何通过程序关闭Android软键盘?

53

我目前正在使用以下代码显示软键盘:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);

因为这个原因,我没有将软键盘绑定到Edittext上,而是使用了上述代码。

现在我想关闭软键盘,所以我目前正在使用下面的代码,但它不起作用。

imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);

有人能建议我使用什么来关闭软键盘吗?


根据下面的回答,我想要让你清楚我没有使用EditText,我使用布局来显示键盘和隐藏键盘。我想发送键盘按键事件到远程区域,因此我没有使用editText。


3
隐藏键盘的代码与显示键盘的代码是相同的。 - Lalit Poptani
是的,但我不知道该用什么来替换它。 - Mak
请查看这个线程。 - Lalit Poptani
这对我不起作用,请阅读上面的编辑。 - Mak
13
在一行代码中隐藏软键盘: ((InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(),0); - Pratik Butani
17个回答

97

我已经测试过,这个是可行的:

...
//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

顺便提一下,你代码的第二个参数不正确,请看这里


6
为什么你使用同一行代码来显示和隐藏? - János
6
根据https://dev59.com/53A65IYBdhLWcg3w6DFO和https://dev59.com/cm445IYBdhLWcg3wq8Lp上的内容,检查键盘是否显示相当困难,所以如果你不知道键盘是否已经显示,切换可能不是一个好主意。 - SOFe
这种方法的问题在于,如果用户隐藏了键盘,第二次调用将再次显示键盘。 - Alexander

41
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditTextName.getWindowToken(), 0);

10
请使用:getCurrentFocus().getWindowToken() - IgorGanapolsky

34

使用以下可行的代码:

InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

它有效了,谢谢。当用户点击登录按钮时,我想隐藏键盘,在这种情况下,这个解决方案比imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)更好。因为在横向手机上,键盘会覆盖登录按钮,用户必须手动隐藏键盘。 - Weiyi

9

如果需要,您可以使用整个类并随处调用KeyboardUtil.hideKeyBoard(context)方法:

public class KeyboardUtil
{
public static void hideKeyboard(Activity activity)
    {
        try
        {
            InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
         }
        catch (Exception e)
        {
            // Ignore exceptions if any
                Log.e("KeyBoardUtil", e.toString(), e);
        }
    }
}

3
一个总是有效且发送标记"HIDE_NOT_ALWAYS"的解决方案,很有趣 :) - holgac
这个很好用。虽然没有必要捕获异常,但是捕获异常看起来很丑陋。 - Abc.Xyz

5

关闭/隐藏Android软键盘

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

it's working for me i hope it's work for you..

打开Android软键盘

 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }

2

user942821的回答:如何隐藏IT工作:

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

但这对于我隐藏它也有效:

imm.toggleSoftInput(0, 0);

你可能还想尝试:

imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

在第一个参数中使用'0'时,有时键盘会在奇怪的情况下切换到错误的位置,我还没有找到如何复制这种情况。我仍在测试最后一个示例,但会在发现更多信息时更新。

有关更多信息,请参见toggleSoftInput文档页面


嗯,InputMethodManager.SHOW_FORCED只是一个表示int值的枚举。所以当然可以工作,但这与将其设置为保持值为0的InputMethodManager.RESULT_UNCHANGED_SHOWN相同。应该始终在需要时使用枚举。请参见http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html#RESULT_UNCHANGED_SHOWN - ForceMagic
当然,我明白了,但是这不应该放在第二个参数中吗?第一个参数是用于showFlags的。public void toggleSoftInput (int showFlags, int hideFlags) 我认为这更合适:imm.toggleSoftInput(InputMethodManager.RESULT_SHOWN, InputMethodManager.RESULT_UNCHANGED_SHOWN); - uowaep
好的观点! :) 实际上,在ShowFlags参数中没有支持0值的枚举标志。再次阅读文档后,我意识到他们指定它可以为0。抱歉,我的错误,尽管我认为没有提供默认枚举标志不是一个好的做法。 - ForceMagic
1
我也一直在研究这个问题,似乎你应该在这里使用魔数。我添加了文档链接。 - uowaep

2

这个很好用

InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);

1
最好能有一个解释。 - gsamaras
并非所有情况下都有效。我发现捕获所有边缘情况是一个比起初看来更大的问题。 - Joshua Pinter

0
InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

0

隐藏键盘,

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);

这里,“mView”可以是屏幕上可见的任何视图


0
你也可以尝试一下
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接