问题
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