How do I close a SearchView programmatically?

后端 未结 13 1719
误落风尘
误落风尘 2020-12-23 19:10

I currently have a SearchView in the action bar of my app. When I click the search icon, the SearchView expands and the keyboard pops up as expecte

13条回答
  •  春和景丽
    2020-12-23 19:27

    There is a simple way to do this:

    @Override
    public void onBackPressed() {
        if (!searchView.isIconified()) {
            searchView.onActionViewCollapsed();
        } else {
            super.onBackPressed();
        }
    }
    

    or use:

    myToolBar.collapseActionView();
    

    This will make the searchView to collapse before you press the back then the back action will be called.

    Both solutions will work.

提交回复
热议问题