How to dismiss keyboard in Android SearchView?

后端 未结 15 1386
萌比男神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:57

    You can use it.

    if (isKeybordShowing(MainActivity.this, MainActivity.this.getCurrentFocus())) {
        onBackPressed();
    }
    
    public boolean isKeybordShowing(Context context, View view) {
        try {
            InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.hideSoftInputFromWindow(view.getWindowToken(), 0);
            return keyboard.isActive();
        } catch (Exception ex) {
            Log.e("keyboardHide", "cannot hide keyboard", ex);
            return false;
        }
    }
    

提交回复
热议问题