Hide the keyboard after a user finishes a query on a SearchView
Hi I am having trouble figuring out how to hide the Android Virtual Keyboard after a search query with a search view. I want the keyboard to disappear when the user presses the return key (ideally when they tap anywhere outside the keyboard but I will settle for the return key for now). I have it working for a query using onQueryTextSubmit working:
public boolean onQueryTextSubmit(String s) {
//Search stuff...
searchView.clearFocus();
return true;
}
however I'd like it to also work if the query is an empty string. The problem is onQueryTextSubmit is not fired if it is an empty string: Android SearchView.OnQueryTextListener OnQueryTextSubmit not fired on empty query string
I'd also not like to use ActionBarSherlock so this solution will not do: Android SearchView empty string
I just want to hide the keyboard after a user searches :( it seems like such a simple problem but is giving me a headache. Any suggestions? Thanks!
If you are using the compatibility library:
MenuItemCompat.collapseActionView(searchItem);//searchItem is an instance of MenuItem
If you are not using the compatibility library:
searchItem.collapseActionView();
Use InputMethodManager:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
for empty query string solution: Android SearchView.OnQueryTextListener OnQueryTextSubmit not fired on empty query string
这工作:
searchView.setQuery("", false);
searchView.setIconified(true);
链接地址: http://www.djcxy.com/p/92958.html