How to hide android keyboard in search view

2020-07-06 04:30发布

问题:

I've used search view in my application. I wonder how to hide the keyboard when i hit the search button on the search view? I have to use the back button to view the results.

回答1:

Here is a similar question: How to dismiss keyboard in Android SearchView?

  @Override
  public boolean onQueryTextSubmit(String query) {
      // Do search.
      searchView.clearFocus();
      return true;
  }


回答2:

Try this code:

private void hideKeyboard(){
    InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

call it on search button click listener



回答3:

you can hide it by

mSearchView.setIconified(true);