问题
Android Studio 2.3.3, Android 4.3.
In my fragment I has searchView component (not in ToolBar). Here my snippet.
fragment_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginEnd="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:background="@drawable/search_view_bg"
app:defaultQueryHint="@string/settings"
app:searchHintIcon="@null" />
</LinearLayout>
In my fragment:
private void init(View view) {
searchView = view.findViewById(R.id.searchView);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
searchView.setSuggestionsAdapter(new SearchCatalogSuggestionsAdapter(getActivity()));
ViewCompat.setLayoutDirection(view.findViewById(R.id.searchView), ViewCompat.LAYOUT_DIRECTION_RTL);
}
Here result when searchView has focus:
And here result when searchView has no focus:
As you can see the hint show only when searchView is on focus. But I need also to show hint when searchView is not in focus. I want hint to be always visible.
P.S. If use method searchView.setIconified(false)
then searchView auto get focus. But I need to get focus only when user click on searchView.
回答1:
I found solution for Android 4.3+:
I use microphone icon , disable click on it and replace it by custom icon(glass)
Hide keyboard after 300 msec (because
onActionViewExpanded()
show soft keyboard)
As result I has:
- Show hint when searchView is not focus
- Show icon on the right side when searchView is on focus and when searchView is NOT in focus
Here my snippet:
Java code:
ImageView voiceButtonIcon = view.findViewById(android.support.v7.appcompat.R.id.search_voice_btn);
voiceButtonIcon.setImageResource(R.drawable.ic_search_black);
voiceButtonIcon.setEnabled(false);
// Workaround! Show hint when searchView has no focus
searchView.onActionViewExpanded();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
searchView.clearFocus();
}
}, 300);
layout xml:
<android.support.v7.widget.SearchView
android:id="@+id/searchView"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginEnd="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:background="@drawable/search_view_bg"
android:theme="@style/SearchViewTheme"
app:defaultQueryHint="@string/settings"
app:searchHintIcon="@null" />
As result: searchView has NO focus:
and searchView HAS in focus:
So hint show always!
来源:https://stackoverflow.com/questions/46681500/android-4-3-not-focus-searchview-not-show-hint