Auto Collapse ActionBar SearchView on Soft Keyboard close

后端 未结 11 988
遥遥无期
遥遥无期 2020-12-07 20:30

I am currently using an ActionBar menu item to display a SearchView in the action bar. When the search menu item is expanded the soft keyboard is displayed which is what I

11条回答
  •  有刺的猬
    2020-12-07 21:11

    For some reason, menuItem.collapseActionView() did not work so I used searchView.setIconified(true) instead.

    This gives the below result as the code sample.

    final MenuItem searchItem = (MenuItem) menu.findItem(R.id.menu_item_search);
    final SearchView searchView = (SearchView) searchItem.getActionView();
    
    searchView.setOnQueryTextFocusChangeListener(new SearchView.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                searchView.setIconified(true);
            }
        }
    });
    

提交回复
热议问题