Auto Collapse ActionBar SearchView on Soft Keyboard close

后端 未结 11 990
遥遥无期
遥遥无期 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:00

    You only need to put the "collapseActionView" attribute in the menu layout

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/menu_item_search"
            android:title="@string/search"
            android:iconifiedByDefault="true"
            android:icon="@drawable/ic_action_search" 
            app:actionViewClass="android.support.v7.widget.SearchView"
            app:showAsAction="ifRoom|collapseActionView"/> <--this one
    </menu>
    

    That will give you the functionality you look for all by itself.Don't forget to call the method "clearFocus" on the SearchView to close the keyboard once you send the query.

    0 讨论(0)
  • 2020-12-07 21:04

    If you want to collapse keyboard when user clicks search icon on keyboard this can be achieved by simple

    inside onquerytextsubmitted {

    searchView.clearfocus()

    }

    0 讨论(0)
  • 2020-12-07 21:08

    Just Override onBackPressed like this:

    @Override
        public void onBackPressed() {
            if (searchView.isShown()){
                searchView.onActionViewCollapsed();  //collapse your ActionView
                searchView.setQuery("",false);       //clears your query without submit
                isClosed = true;                     //needed to handle closed by back
            } else{
                super.onBackPressed();
            }
        }
    

    and your onCreateOptionsMenu would inflate the mSearchView like this:

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            getMenuInflater().inflate(R.menu.menu_search, menu);
            mSearchView = (SearchView) menu.findItem(R.id.menu_action_search).getActionView();
            mSearchView.setOnQueryTextListener(this);
            mSearchView.setOnSearchClickListener(this);
            mSearchView.setOnCloseListener(this);
            isClosed = true;
            return true;
        }
    

    have you class implement the following like this:

    public class myActivity extends FragmentActivity implements
        SearchView.OnQueryTextListener, View.OnClickListener, SearchView.OnCloseListener {
    

    which you will also need:

    @Override
    public void onClick(View view) {
        isClosed = false;
    }
    
    @Override
    public boolean onClose() {
        isClosed = true;
        return false;
    }
    

    You will need to make "mSearchView" and "isClosed" both global variables to the activity.

    0 讨论(0)
  • 2020-12-07 21:08

    You need to call setIconified twice.

    To actually collapse your search view and close the keyboard.
    With first call text of search view is cleared with second call keyboard and search view get closed.

    0 讨论(0)
  • 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);
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题