Android 4.3:不聚焦搜索视图不显示提示

Android 4.3:不聚焦搜索视图不显示提示,android,searchview,Android,Searchview,安卓工作室2.3.3,安卓4.3 在我的片段中,我有searchView组件(不在工具栏中)。这是我的片段 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

安卓工作室2.3.3,安卓4.3

在我的片段中,我有searchView组件(不在工具栏中)。这是我的片段

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>
searchView具有焦点时,会出现以下结果:

searchView没有焦点时,结果如下:

正如您所看到的,提示仅在searchView处于焦点时显示。 但我还需要在searchView未处于焦点时显示提示。我希望提示始终可见


注意:如果使用方法
searchView.setIconified(false)
searchView自动获取焦点。但只有当用户点击searchView时,我才需要获得关注。我找到了Android 4.3+的解决方案:

  • 我使用麦克风图标,禁用并单击它,然后将其替换为自定义图标(玻璃)

  • 300毫秒后隐藏键盘(因为
    onActionViewExpanded()
    show软键盘)

  • 因此,我:

  • 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);
    
            // Workaround! Show hint when searchView has no focus
            searchView.onActionViewExpanded();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    searchView.clearFocus();
                }
            }, 300);
    
    布局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" />
    

    并且,searchView重点关注:


    所以,我要永远展示

    添加此行后重试-searchView.setSelected(true);没有帮助。同样的结果。没有关于非焦点状态的提示。请尝试在包装的
    EditText
    EditText EditText=(EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search\u src\u text)上请求焦点editText.post(()->editText.requestFocus())
    final EditText EditText=searchView.findviewbyd(android.support.v7.appcompat.R.id.search\u src\u text);editText.post(新Runnable(){@Override public void run(){editText.requestFocus();}})没有帮助。同样的结果
    
    <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" />