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:30

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;
            }
        });
查看更多
迷人小祖宗
3楼-- · 2019-01-14 00:31

I used ActionBarSherlock 4.3 and I have a ProgressDialog. When I dismiss it in postExecute method, Searchview gain focus. To fix that:

//Handle intent and hide soft keyboard
    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent(intent);
        searchView.setQuery("", false);
        searchView.setIconified(true);
        searchView.clearFocus();
    }

    /*When dismiss ProgressDialog, searchview gain focus again and shows the keyboard. I  call clearFocus() to avoid it.*/

    AsyncTask.onPostExecute(){
      if(pd!=null)
        pd.dismiss();
      if(searchView!=null)
        searchView.clearFocus();
    }

Hope it helps.

查看更多
别忘想泡老子
4楼-- · 2019-01-14 00:33

just return false on onQueryTextSubmit just like below

@Override
public boolean onQueryTextSubmit(String s) {
     return false;
}
查看更多
走好不送
5楼-- · 2019-01-14 00:33

Somehow it works if you call

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

and then

otherWidget.requestFocus();
查看更多
forever°为你锁心
6楼-- · 2019-01-14 00:35

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);
 }
查看更多
beautiful°
7楼-- · 2019-01-14 00:41

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.

查看更多
登录 后发表回答