Android 4.3:不聚焦 searchView 不显示提示 [英] Android 4.3: Not focus searchView not show hint
问题描述
Android Studio 2.3.3、Android 4.3.
在我的片段中,我有 searchView 组件(不在工具栏中).这是我的片段.
fragment_layout.xml:
<android.support.v7.widget.SearchViewandroid: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>
在我的片段中:
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);}
这里是 searchView 获得焦点时的结果:
这里是 searchView 没有焦点时的结果:
如您所见,提示仅在 searchView 处于焦点上时显示.但我还需要在 searchView 不在焦点时显示提示.我希望提示始终可见.
附言如果使用方法 searchView.setIconified(false)
然后 searchView 自动获得焦点.但我只需要在用户点击 searchView 时获得焦点.
我找到了适用于 Android 4.3+ 的解决方案:
我使用麦克风图标,禁用点击它并用自定义图标(玻璃)替换
300 毫秒后隐藏键盘(因为
onActionViewExpanded()
显示软键盘)
结果我有:
- 当 searchView 不是焦点时显示提示
- 当 searchView 处于焦点且 searchView 未处于焦点时,在右侧显示图标
这是我的片段:
Java 代码:
ImageView voiceButtonIcon = view.findViewById(android.support.v7.appcompat.R.id.search_voice_btn);voiceButtonIcon.setImageResource(R.drawable.ic_search_black);voiceButtonIcon.setEnabled(false);//解决方法!当 searchView 没有焦点时显示提示searchView.onActionViewExpanded();new Handler().postDelayed(new Runnable() {@覆盖公共无效运行(){searchView.clearFocus();}}, 300);
布局xml:
和 searchView 具有焦点:
所以提示总是显示!
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.
解决方案 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!
这篇关于Android 4.3:不聚焦 searchView 不显示提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!