Android SearchView Hide Keyboard on Start up

牧云@^-^@ 提交于 2019-12-10 14:49:28

问题


I'm having a minor issue I'm trying to solve. When I open my application, the keyboard shows to enter a query for the search view. However I just want the keyboard to appear when I click on the search view. How do I fix this?

Thanks!


回答1:


This worked for me.

/* Code used to hide focus */

searchView =(SearchView)view.findViewById(R.id.searchView);
searchView.setFocusable(false);



回答2:


Use this attributes in your layout tag in XML file:

android:focusable="true"
android:focusableInTouchMode="true"

or in manifest xml for your Activity add the property:

android:windowSoftInputMode="stateAlwaysHidden"



回答3:


This works for me:

searchView =(SearchView)view.findViewById(R.id.searchView);
searchView.clearFocus();



回答4:


This is work for me: clear focus in onResume() of activity or fragment

objSearchView=(SearchView)view.findViewById(R.id.searchView);

 @Override
    public void onResume() {
        super.onResume();
        objSearchView.clearFocus();
    }



回答5:


for me I have to call both setFocusable(false) and clearFocus()

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        . . .
        searchView = (SearchView) findViewById(R.id.search_view);
        searchView.setFocusable(false);
        . . .
    }

@Override
    protected void onResume() {
        super.onResume();
        . . .
        searchView.clearFocus();
        . . .
    }



回答6:


Make sure you do not use this:

searchInput = (SearchView) layout.findViewById(R.id.search);
searchInput.onActionViewExpanded();//<<<<------------------ don't use it.


来源:https://stackoverflow.com/questions/35301094/android-searchview-hide-keyboard-on-start-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!