How to dismiss/close/collapse SearchView in ActionBar in MainActivity?

前端 未结 6 1985
无人共我
无人共我 2021-01-07 17:54

I\'m trying to clear and close SearchView after entering a value. I found a solution but it closes the search and won\'t perform any action if I try to search again.

6条回答
  •  终归单人心
    2021-01-07 18:26

    In menu_main.xml add :

    
    

    and in onCreateOptionsMenu

    final  MenuItem miSearch = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) miSearch.getActionView();
        searchView.setQueryHint("Searh For");
        searchView.setOnQueryTextListener(newSearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Toast.makeText(context,query, Toast.LENGTH_LONG).show();
                // Here Put Your Code.
                //searchView.onActionViewCollapsed();
                miSearch.collapseActionView();
                return false;
            }
    
            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    

提交回复
热议问题