how to get the events of searchview in android

后端 未结 6 869
傲寒
傲寒 2020-12-15 04:58

I\'m using a search view in my application. Now i just want to get the text typed in the SearchView text box and display it on another textview. If i typed the text and clic

6条回答
  •  醉梦人生
    2020-12-15 05:35

    The above answer is good but not complete actually you need to set an action listener for your Search view . you can do this in two ways create a class that implements the necessary classes to be an OnQueryTextListener and make a new object of that and use it as your search view query text listener or use the following compact form:

            SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    callSearch(query);
                    return true;
                }
    
                @Override
                public boolean onQueryTextChange(String newText) {
    //              if (searchView.isExpanded() && TextUtils.isEmpty(newText)) {
                        callSearch(newText);
    //              }
                    return true;
                }
    
                public void callSearch(String query) {
                    //Do searching
                }
    
            });
    

提交回复
热议问题