How to dismiss keyboard in Android SearchView?

2019-01-14 00:01发布

I have a searchView in the ActionBar. I want to dismiss the keyboard when the user is done with input. I have the following queryTextListener on the searchView

final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() { 
    @Override 
    public boolean onQueryTextChange(String newText) { 
        // Do something 
        return true; 
    } 

    @Override 
    public boolean onQueryTextSubmit(String query) {

        showProgress();
        // Do stuff, make async call

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        return true; 
    } 
};

Based on similar questions, the following code should dismiss the keyboard, but it doesn't work in this case:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

I've also tried:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);

Neither one works. I'm not sure if this is a Honeycomb specific problem or if it's related to the searchView in the ActionBar, or both. Has anyone gotten this working or know why it does not work?

12条回答
虎瘦雄心在
2楼-- · 2019-01-14 00:20

Edit: I added the better solution on top, but also kept the old answer as a reference.

 @Override
        public boolean onQueryTextSubmit(String query) {

                  searchView.clearFocus();
            return false;
        }

Original Answer: I programmed using a setOnQueryTextListener. When the searchview is hidden the keyboard goes away and then when it is visible again the keyboard does not pop back up.

    //set query change listener
     searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
        @Override
        public boolean onQueryTextChange(String newText) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            /**
             * hides and then unhides search tab to make sure keyboard disappears when query is submitted
             */
                  searchView.setVisibility(View.INVISIBLE);
                  searchView.setVisibility(View.VISIBLE);
            return false;
        }

     });
查看更多
一纸荒年 Trace。
3楼-- · 2019-01-14 00:20

Below Code is working for me to hide keyboard in Searchview

MenuItem searchItem = baseMenu.findItem(R.id.menuSearch);

edtSearch= (EditText) searchItem.getActionView().findViewById(R.id.search_src_text);

MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
                        @Override
                        public boolean onMenuItemActionExpand(MenuItem item) {
                            edtSearch.post(new Runnable() {
                                @Override
                                public void run() {
                                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                    imm.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
                                }
                            });
                            return true;
                        }

                        @Override
                        public boolean onMenuItemActionCollapse(MenuItem item) {
                            return true;
                        }
                    });
查看更多
倾城 Initia
4楼-- · 2019-01-14 00:21

Why dont you try this? When you touch the X button, you trigger a focus on the searchview no matter what.

ImageView closeButton = (ImageView)searchView.findViewById(R.id.search_close_btn);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et = (EditText) findViewById(R.id.search_src_text);
                et.setText("");`enter code here`
                searchView.setQuery("", false);
                searchView.onActionViewCollapsed();
                menuSearch.collapseActionView();
            }
        });
查看更多
Fickle 薄情
5楼-- · 2019-01-14 00:23

Simple, straight to the point and clean:

  @Override
  public boolean onQueryTextSubmit(String query) {
      // your search methods
      searchView.clearFocus();
      return true;
  }
查看更多
甜甜的少女心
6楼-- · 2019-01-14 00:27

I was trying to do something similar. I needed to launch the SearchActivity from another Activity and have the search term appear on the opened search field when it loaded. I tried all the approaches above but finally (similar to Ridcully's answer above) I set a variable to SearchView in onCreateOptionsMenu() and then in onQueryTextSubmit() called clearFocus() on the SearchView when the user submitted a new search:

private SearchView searchView;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_menu, menu);
    searchView = (SearchView) menu.findItem(R.id.menu_search)
            .getActionView(); // set the reference to the searchView
    searchView.setOnQueryTextListener(this); 
    searchMenuItem = (MenuItem) menu.findItem(R.id.menu_search); 
    searchMenuItem.expandActionView(); // expand the search action item automatically
    searchView.setQuery("<put your search term here>", false); // fill in the search term by default
    searchView.clearFocus(); // close the keyboard on load
    return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
    performNewSearch(query);
    searchView.clearFocus();
    return true;
}
查看更多
成全新的幸福
7楼-- · 2019-01-14 00:28

In a tablet app I'm working on with a dual pane activity, I've wrote only

f.getView().requestFocus(); // f is the target fragment for the search

and that was enough to dismiss the soft keyboard after a search. No need to use InputMethodManager

查看更多
登录 后发表回答