138

我有一个Activity带有EditText一个按钮和一个ListView。目的是在 中键入搜索屏幕EditText,按下按钮并让搜索结果填充此列表。

这一切都很好,但虚拟键盘的行为很奇怪。

如果我点击EditText,我会得到虚拟键盘。如果我单击虚拟键盘上的“完成”按钮,它就会消失。但是,如果我在单击虚拟键盘上的“完成”之前单击搜索按钮,则虚拟键盘会保留下来,我无法摆脱它。单击“完成”按钮不会关闭键盘。它将“完成”按钮从“完成”更改为箭头并保持可见。

谢谢你的帮助

4

14 回答 14

312
InputMethodManager inputManager = (InputMethodManager)
                                  getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                     InputMethodManager.HIDE_NOT_ALWAYS);

我把这个放在onClick(View v)事件之后。

您需要导入android.view.inputmethod.InputMethodManager

单击按钮时键盘会隐藏。

于 2011-11-21T19:34:58.537 回答
60
mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            // hide virtual keyboard
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(), 
                                      InputMethodManager.RESULT_UNCHANGED_SHOWN);
            return true;
        }
        return false;
    }
});
于 2010-09-17T15:47:35.287 回答
32

使用下面的代码

your_button_id.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        try  {
            InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        } catch (Exception e) {

        }
    }
});
于 2014-12-01T12:43:31.107 回答
13

您应该OnEditorActionListener为您的 EditView实施

public void performClickOnDone(EditView editView, final View button){
    textView.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(EditView v, int actionId, KeyEvent event) {
            hideKeyboard();
            button.requestFocus();
            button.performClick();
            return true;
        }
    });

你通过以下方式隐藏键盘:

public void hideKeybord(View view) {
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),
                                  InputMethodManager.RESULT_UNCHANGED_SHOWN);
}

您还应该使用隐藏在按钮中的键盘onClickListener

现在单击虚拟键盘和按钮上的“完成”将执行相同的操作 - 隐藏键盘并执行单击操作。

于 2010-08-03T20:45:02.827 回答
12

在您的情况下,由于您只有一个 EditText,我相信下面的解决方案是最好的解决方案。如果您有多个 EditText 组件,那么您需要专注于要关闭的 EditText,或者为每个 EditText 调用波纹管函数。不过,对你来说,这非常有效:

editText.onEditorAction(EditorInfo.IME_ACTION_DONE);

基本上,EditText 已经有一个函数,用于处理使用键盘后要做什么。我们只是传递了我们想要关闭键盘的信息。

于 2017-03-09T11:13:41.097 回答
12

对于活动,

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

对于片段,使用getActivity()

getActivity().getSystemService();

getActivity().getCurrentFocus();

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
于 2017-11-16T07:34:31.787 回答
11

在按钮单击事件中添加以下代码:

InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
于 2015-04-09T10:06:34.007 回答
7

这个解决方案非常适合我:

private void showKeyboard(EditText editText) {
    editText.requestFocus();
    editText.setFocusableInTouchMode(true);
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN);
    editText.setSelection(editText.getText().length());
}

private void closeKeyboard() {
    InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
于 2017-07-20T17:02:00.230 回答
4

试试这个...

  1. 用于显示键盘

    editText.requestFocus();
    InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    
  2. 对于隐藏键盘

    InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); 
    inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    
于 2016-11-09T09:39:50.760 回答
1
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}
于 2017-04-02T22:32:11.267 回答
1

科特林示例:

val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

从片段:

inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)

从活动:

inputMethodManager.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
于 2018-08-10T11:10:24.830 回答
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);
}
于 2016-10-26T05:49:43.883 回答
0

崩溃空点异常修复:我遇到了当用户单击按钮时键盘可能无法打开的情况。您必须编写一个 if 语句来检查 getCurrentFocus() 是否为空:

            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if(getCurrentFocus() != null) {
            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
于 2017-04-29T23:29:27.557 回答
-2

如果设置android:singleLine="true",按钮自动隐藏键盘¡

于 2016-04-14T09:16:51.167 回答