How to close a SearchView programmatically on back pressed?

后端 未结 8 1928
余生分开走
余生分开走 2021-01-07 07:39

I have the same question which I found here which I will re-iterate because the solution is not 100% exactly what I need:

I currently have a SearchVie

相关标签:
8条回答
  • 2021-01-07 08:05

    try

    @Override
    public void onBackPressed() {
    
    View view = this.getCurrentFocus();
    if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    if (!searchView.isIconified()) {
        searchView.setIconified(true);
    } else {
        super.onBackPressed();
    }
    }
    
    0 讨论(0)
  • 2021-01-07 08:06

    Add a method to close the keyboard within onBackPressed() here is the code to hide keyboard.

    private void hideKeyboard() {
    InputMethodManager imm = (InputMethodManager) 
        getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mInputField.getWindowToken(), 0);
    }
    
    0 讨论(0)
  • 2021-01-07 08:16

    I did it like this in the activity where the SearchView is located

    @Override
        public void onBackPressed() {
            super.onBackPressed();
            if (!searchView.isIconified()) {
                searchView.setIconified(true);
            }
        }
    
    0 讨论(0)
  • 2021-01-07 08:17
    public class MainActivity extends AppCompatActivity {
        MenuItem menuItemSearch;
    
        @Override
        protected void onResume() {
          if(menuItemSearch!=null)
            MenuItemCompat.collapseActionView(menuItemSearch);  // while activity begins, searchView is closed if remained open.
        }
    
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
          menuItemSearch = menu.findItem(R.id.menuSearch);
        }
    }
    
    0 讨论(0)
  • 2021-01-07 08:24

    Based on @Archana answer, the onBackPressed() should be like :

    @Override
    public void onBackPressed() {    
        hideSoftKeyboard();
        if (!searchView.isIconified()) {
            searchView.setIconified(true);
        } else {
            super.onBackPressed();
        }
    }
    
    private void hideSoftKeyboard(){
        View view = activity.getCurrentFocus ();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService (Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow (view.getWindowToken (), 0);
        }
    }
    

    or you can also override the onKeyDown()

    @Override
    public boolean onKeyDown (int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            hideSoftKeyboard ();
            if (! searchView.isIconified ()) {
                searchView.setIconified (true);
            } 
            return true;
        }
        return super.onKeyDown (keyCode, event);
    }
    
    private void hideSoftKeyboard() {
        View view = activity.getCurrentFocus ();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService (Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow (view.getWindowToken (), 0);
        }
    }
    
    0 讨论(0)
  • 2021-01-07 08:26
    @Override
    public void onBackPressed() {    
        if (!searchView.isIconified()) {
            searchView.setIconified(true);
            searchView.onActionViewCollapsed()
        } else {
            super.onBackPressed();
        }
    }
    

    below method is Clear your text Only

    searchView.setIconified(true); 
    

    below This methods is close your search view

    searchView.onActionViewCollapsed()
    
    0 讨论(0)
提交回复
热议问题