【问题标题】:SearchView: when click Done button nothing happensSearchView:单击完成按钮时没有任何反应
【发布时间】:2017-11-16 10:17:10
【问题描述】:

这里是sn-p:

 searchViewEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
        searchViewEditText.setSingleLine(true);

当键盘打开时,会显示完成按钮。 好的。

这里的结果:

但是当我点击按钮 Done 时,什么也没有发生。我希望键盘会被隐藏。

【问题讨论】:

标签: android


【解决方案1】:

您可以使用它(设置在 EditText 上执行操作时调用的特殊侦听器),它适用于 DONE 和 RETURN:

max.setOnEditorActionListener(new OnEditorActionListener() {
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
            Log.i(TAG,"Enter pressed");
        }    
        return false;
    }
});

【讨论】:

    【解决方案2】:

    我认为您必须以编程方式关闭键盘。为此,请将以下逻辑添加到 TextView 侦听器:

    editText = (EditText) findViewById(R.id.edit_text);
    
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                   //Replace <<active view>> With the active view
                    inputMethodManager.hideSoftInputFromWindow(<<active view>>.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
                return false;
            }
        });
    

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多