Unable to hide the virtual keyboard of SearchView iconfiedbydefault(false)

前端 未结 7 2048
轮回少年
轮回少年 2021-01-18 21:11

I have a search view which is set as expanded by default with default search query but i don\'t want the virtual keyboard.In below code i tried to hide keyboard in o

相关标签:
7条回答
  • 2021-01-18 21:54

    add the below line in the manifest for particular Activity.

     android:windowSoftInputMode="adjustPan|stateHidden"
    
    0 讨论(0)
  • 2021-01-18 22:04

    u just have to use: "object(edittext, searchview, etc)".clearfocus() ;

    use it after u generate a search or an action. Example: in the method OnQueryTextListener, after that i use a search. For searchview.

    0 讨论(0)
  • 2021-01-18 22:06

    Inspired by Parnit's answer, I've found a better method, which also works and is more beautiful:

    mSearchView.clearFocus();
    
    0 讨论(0)
  • 2021-01-18 22:09

    In Android Manifest:

     android:windowSoftInputMode="adjustPan|stateHidden"
    

    In class open and close the keyboard:

       @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action buttons
          switch(item.getItemId()) {
    case R.id.search:
             //TODO Whatever
              search.clearFocus();
             //Open and close the  keyboard
              InputMethodManager imm = (InputMethodManager)MyApplication.getAppContext().getSystemService(
                      Context.INPUT_METHOD_SERVICE);
              imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
              imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
              return true;
    
    0 讨论(0)
  • 2021-01-18 22:10

    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;
            }
    
         });
    
    0 讨论(0)
  • 2021-01-18 22:13

    try

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    
    0 讨论(0)
提交回复
热议问题