java 检测键盘搜索按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9948127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:01:02  来源:igfitidea点击:

detect keyboard search button

javaandroidandroid-edittextandroid-softkeyboard

提问by Fcoder

I change android soft keyboard to show search icon rather than Enter icon. My question is: how i can detect that user click on that search button?

我更改了 android 软键盘以显示搜索图标而不是 Enter 图标。我的问题是:我如何检测该用户点击该搜索按钮?

回答by Arpit Garg

In the edittext you might have used your input method options to search.

在 edittext 中,您可能使用了输入法选项进行搜索。

<EditText
      android:imeOptions="actionSearch"
      android:inputType="text"/>

Now use the Editor listener on the EditText to handle the search event as shown below:

现在使用 EditText 上的 Editor 侦听器来处理搜索事件,如下所示:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
           // Your piece of code on keyboard search click 
            return true;
        }
        return false;
    }
});

回答by Saad Mahmud

You can use OnQuerySubmitlistener to get keyboard Search button click event.

您可以使用OnQuerySubmit侦听器来获取键盘搜索按钮单击事件。

Here's how,

就是这样,

searchView.setOnQueryTextListener(queryTextListener);

SearchView.OnQueryTextListener queryTextListener
        = new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String s) {
        showLog("searchClickListener onQueryTextSubmit");
        getPhotos(searchView.getQuery().toString());
        return true;
    }

    @Override
    public boolean onQueryTextChange(String s) {
        return false;
    }
};