How to dismiss keyboard in Android SearchView?

后端 未结 15 1340
萌比男神i
萌比男神i 2020-12-05 06:27

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



        
相关标签:
15条回答
  • 2020-12-05 06:34

    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;
                            }
                        });
    
    0 讨论(0)
  • 2020-12-05 06:38

    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;
    }
    
    0 讨论(0)
  • 2020-12-05 06:38

    Two solutions that worked for me, the first one using the SearchView instance:

    private void hideKeyboard(){
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
    }
    

    Second solution:

    private void hideKeyboard(){
                InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
                inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
     }
    
    0 讨论(0)
  • 2020-12-05 06:42

    For me, the following works:

    In my activity I have a member variable

    private SearchView mSearchView;
    

    In onCreateOptionsMenu() I set that variable like so:

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.library, menu);
        mSearchView = (SearchView)menu.findItem(R.id.miSearch).getActionView();
        mSearchView.setOnQueryTextListener(this);
        return true;
    }   
    

    In the QueryTextListener at last, I do this:

    mSearchView.setQuery("", false);
    mSearchView.setIconified(true); 
    

    I had a look at the source code of SearchView, and if you do not reset the query text to an empty string, the SearchView just does that, and does not remove the keyboard neither. Actually, drilling down deep enough in the source code, it comes to the same, yuku suggested, but still I like my solution better, as I do not have to mess around with those low level stuff.

    0 讨论(0)
  • 2020-12-05 06:47

    For me, none of the above was working on the first submit. It was hiding and then immediately re-showing the keyboard. I had to post the clearFocus() on the view's handler to make it happen after it was done with everything else.

    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    if (!"".equals(query)) {
                        mSearchView.post(new Runnable() {
                            @Override
                            public void run() {
                                mSearchView.clearFocus();
                            }
                        });
                    }
                    return true;
                }
    
                @Override
                public boolean onQueryTextChange(String newText) {
                    return false;
                }
            });
    
    0 讨论(0)
  • 2020-12-05 06:49

    just return false on onQueryTextSubmit just like below

    @Override
    public boolean onQueryTextSubmit(String s) {
         return false;
    }
    
    0 讨论(0)
提交回复
热议问题