繁体   English   中英

SearchView:单击“完成”按钮时,没有任何反应

[英]SearchView: when click Done button nothing happens

以下代码段:

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

结果,当键盘打开时,显示完成按钮。 好。

结果如下:

DONE

但是,当我单击“完成”按钮时,没有任何反应。 我希望键盘会隐藏起来。

您可以使用它(设置在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;
    }
});

我认为您将必须以编程方式关闭键盘。 这样做,将以下逻辑添加到TextView Listener:

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

我希望这可以帮助你 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM