Android 4.3: Not focus searchView not show hint

浪子不回头ぞ 提交于 2019-12-23 21:25:31

问题


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+:

  1. I use microphone icon , disable click on it and replace it by custom icon(glass)

  2. Hide keyboard after 300 msec (because onActionViewExpanded() show soft keyboard)

As result I has:

  1. Show hint when searchView is not focus
  2. 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

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