detect keyboard search button

2019-02-16 10:23发布

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?

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-16 11:10

You can use OnQuerySubmit listener to get keyboard Search button click event.

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;
    }
};
查看更多
可以哭但决不认输i
3楼-- · 2019-02-16 11:21

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

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

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

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;
    }
});
查看更多
登录 后发表回答