如何在Android中输入文本后隐藏键盘?

76

我有一个位于父视图底部的EditText和按钮。

当我在其中输入文本并按下保存数据的按钮时,虚拟键盘不会消失。

请问有人可以指导我如何隐藏键盘吗?


你是不是指的是EditText(而不是TextView)? - SMBiggs
15个回答

149

你可能还想在EditText中定义imeOptions。这样,当你按下“完成”时,键盘就会消失:

<EditText
    android:id="@+id/editText1"
    android:inputType="text"
    android:imeOptions="actionDone"/>

49
只有在实现onEditorAction([...])时不消耗事件,才能保持这种情况。返回“true”将防止键盘正确隐藏。 - Eric Tobias
9
当使用 actionDone 或 actionSearch 时,软键盘会在按下“完成”或“搜索”后消失,但您需要在 onEditorActionListener 中返回 false ,以实现该功能。请注意,翻译不包括解释和其他内容。 - alexhilton
7
这个和 android:singleLine="true"。 (说明:这句话比较短且缺乏上下文,我已经尽力保持原意进行翻译) - Simas
1
@Borja singleLine已被替换为maxLines=1。应该可以正常工作。如果不能,请创建一个单独的问题。 - Simas
1
对我来说不起作用。当我点击外部时,实际上什么都没有发生。无论我做什么,键盘都不会消失。 - VanessaF
显示剩余4条评论

132

这应该可以工作。

InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); 

只需确保 this.getCurrentFocus() 不返回 null,否则如果没有焦点则会返回 null。


在onCreate上获得以下错误:无法从类型中的非静态方法getSystemService(String)进行静态引用 - UMAR-MOBITSOLUTIONS
32
输入法管理器 imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); 正在工作中... - UMAR-MOBITSOLUTIONS
3
不需要使用此方法隐藏软键盘。请查看下面@angelrh的答案以及评论,其中建议在OnEditorActionListener中返回false,并在xml中将您的编辑文本标记为singleLine = true。 - Nilesh
getCurrentFocus() 获取 null 值。 - android_jain
@android_jain 这是因为没有任何东西拥有焦点。 - Ryan Alford

28
   mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    // do something, e.g. set your TextView here via .setText()
                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    return true;
                }
                return false;
            }
        });

以及在XML中

  android:imeOptions="actionDone"

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

10

我没有看到任何人使用这种方法:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean focused) {
        InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (focused)
            keyboard.showSoftInput(editText, 0);
        else
            keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
});

然后只需请求EditText获取焦点:

editText.requestFocus();

5

在EditText操作监听器中包含的解决方案:

public void onCreate(Bundle savedInstanceState) {
    ...
    ...
    edittext = (EditText) findViewById(R.id.EditText01);
    edittext.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });
    ...
    ...
}

3
我发现这是因为我的EditText在按下回车键后没有自动消失。
以下是我的原始代码:
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))) {

            // Do stuff when user presses enter

            return true;

        }

        return false;
    }
});

我通过删除该行代码解决了这个问题。
return true;

在用户按下回车键后完成操作。希望这能帮助到一些人。

3
只需编写以下两行代码,即可实现回车选项的功能。
InputMethodManager inputManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);

2
在过去的几天里一直在苦苦挣扎,最终找到了一个非常好用的解决方案。当用户点击EditText外部的任何地方时,软键盘将被隐藏。
代码发布在这里:hide default keyboard on click in android

2

您可以在顶部看到标记的答案。但是我使用了getDialog().getCurrentFocus(),并且工作得很好。我发布这个答案,因为我无法在我的oncreatedialog中输入"this"

所以这就是我的答案。如果您尝试了标记的答案但没有成功,您可以简单地尝试这个:

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

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