首页 > 解决方案 > 将 SearchView 与自定义 searchSuggestAuthority 一起使用时来自系统的不需要的建议

问题描述

我的应用程序的 ActionBar 中有一个 android.widget.SearchView。当我开始输入时,它会显示我的 ContentProvider 连接到 Room 数据库的结果。这一切在模拟器上都可以正常工作,但是在我的真实设备上,当我触摸 SearchView 文本字段时,我会立即看到我的结果,然后弹出窗口被这个奇怪的弹出窗口替换,显示两个电话号码(这是我知道的号码)。如果我开始输入,这个框就会消失,正确的结果会再次出现。

用奇怪的弹出窗口搜索

对于可能导致这种情况的原因,我束手无策。这是与 SearchView 相关的代码。

MainActivity.kt

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.bottom_nav_menu, menu)

    var searchItem = menu.findItem(R.id.ItemSearchView);
    var searchView : SearchView = searchItem.actionView as SearchView;
    val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
    val searchableInfo = searchManager.getSearchableInfo(componentName)

    searchView.setSearchableInfo(searchableInfo)
    searchView.isIconifiedByDefault = false
    searchView.maxWidth = 10000;
    searchView.onFocusChangeListener = View.OnFocusChangeListener { view, b ->
        Log.i(TAG, "SearchView focus is ${b}")
    }

    return super.onCreateOptionsMenu(menu)
}

可搜索的.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/honey_where"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
    android:searchSuggestAuthority=
           "com.example.contentProviders.SearchSuggestionProvider"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:searchSuggestThreshold="0"
/>

bottom_nav_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/navigation_locationlist"
    android:iconTint="@color/colorPrimary"
    android:icon="@android:drawable/ic_menu_mapmode"
    android:title="@string/title_locations" />

<item
    android:id="@+id/ItemSearchView"
    android:icon="@drawable/ic_icon"
    android:iconTint="@color/colorAccent"
    android:iconifiedByDefault="false"
    android:queryHint="Search"
    android:theme="@style/AppSearchView"
    android:inputType="textAutoComplete"
    app:actionLayout="@layout/searchview"
    app:showAsAction="always"
    android:title="" />

<item
    android:id="@+id/navigation_settings"
    android:icon="@android:drawable/ic_menu_manage"
    android:title="@string/title_settings" />
</menu>

AndroidManifest.xml

  <provider
        android:name=".contentProviders.SearchSuggestionProvider"
        android:authorities=
            "com.example.contentProviders.SearchSuggestionProvider"
        android:enabled="true"
        android:exported="false"></provider>

   <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme"
        tools:ignore="LockedOrientationActivity">
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    </activity>

这是 SearchView 在两个字符后正常工作的示例。触摸它会显示奇怪的弹出窗口,一个字符什么都不做,两个字符开始正常运行,这在模拟器中也可以正常工作: SearchView 正常运行

我已经尝试了我能想到的一切,但我完全坚持这一点。任何人都可以提供的任何帮助,即使只是一个正确方向的提示,将不胜感激。

TL; DR 我试图找出覆盖我的自定义搜索的弹出窗口的来源,知道这至少会帮助我寻找摆脱它的方法。如果有人知道如何摆脱它,那就太棒了。

更新:2020 年 5 月 29 日:

我尝试单步执行 SearchView 源代码以查看发生了什么,但“交换”似乎发生在 SearchSuggestion 提供其建议之后。是否有“更深”的地方可能在这里激活?

附带说明一下,这是在 OnePlus 6T 上,以防他们可能添加了某种自定义代码。

标签: androidkotlinsearchviewcustom-search-provider

解决方案


我终于能够通过使用以下方法解决这个问题:

searchView.importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS

推荐阅读