SearchView需要点击两次才能打开

SearchView needs to be clicked twice to open

我正在关注此 Android tutorial 以实现搜索视图。解决了一些问题后,我开始工作了。但是,需要单击两次搜索视图才能打开 editText。知道发生了什么事吗?

过滤器 class:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.toolbar_menu_filter, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.menu_item_search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    searchView.setQueryHint(getResources().getString(R.string.search_hint));

    return super.onCreateOptionsMenu(menu);
}

菜单.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".FilterUI">

<item android:id="@+id/menu_item_search"
    android:title="Search"
    android:icon="@drawable/ic_search_white"
    app:actionViewClass="android.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView"/>

<item android:id="@+id/menu_item_options"
    android:icon="@drawable/ic_clear_all_white"
    app:showAsAction="ifRoom"
    android:title="@string/filter_default"/>

清单文件

<activity android:name=".Activities.FilterUI"
        android:screenOrientation="portrait"
        android:configChanges="orientation"
        android:windowSoftInputMode="adjustNothing">

        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />

    </activity>

    <activity android:name=".Activities.SearchActivity">

        <intent-filter>
            <action android:name="android.intent.action.SEARCH"/>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

可能来不及回答了,但是,让我们完成吧。就像我说的,最好使用:

app:actionViewClass="android.support.v7.widget.SearchView"

为了更好的兼容性等。但重点是,在新的 AndroidX 中,我们不能使用 android.support,因为我们迁移到 AndroidX,并且只有:

app:actionViewClass="android.widget.SearchView"

可用。但是,让我们看看有关最新 API (AndroidX) 等的文档说明:

Note: This class is included in the support library for compatibility with API level 7 and higher. If you're developing your app for API level 11 and higher only, you should instead use the framework android.widget.SearchView class.

所以在 AndroidX 中,我发现:

app:actionViewClass="androidx.appcompat.widget.SearchView"

要使用但您仍然可以使用:android.widget.SearchView