How to dismiss keyboard in Android SearchView?

后端 未结 15 1387
萌比男神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 07:01

    Why dont you try this? When you touch the X button, you trigger a focus on the searchview no matter what.

    ImageView closeButton = (ImageView)searchView.findViewById(R.id.search_close_btn);
            closeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText et = (EditText) findViewById(R.id.search_src_text);
                    et.setText("");`enter code here`
                    searchView.setQuery("", false);
                    searchView.onActionViewCollapsed();
                    menuSearch.collapseActionView();
                }
            });
    

提交回复
热议问题