我在主要活动的操作栏中有一个搜索小部件。我想在用户键入至少 2 个字符后显示建议。
我知道这可以通过设置 android:searchSuggestThreshold="2" 来完成,但它似乎不起作用,因为当用户键入零个或多个字符时会调用 SuggestionContentProvider。
即使用户单击搜索图标(0 个字符)以展开小组件,也会调用内容提供程序。
这是我可搜索的.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/search_hint"
android:searchSettingsDescription="@string/search_settings"
android:searchSuggestAuthority="com.studiofrenetic.rmfb.Services.SearchContentProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestThreshold="2">
</searchable>
我的清单中只有一个可搜索的活动.xml:
<activity
android:name="com.studiofrenetic.rmfb.MainActivity"
android:screenOrientation="portrait"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
在创建选项菜单中设置可搜索信息的代码:
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
mSearchView.setOnQueryTextListener(this);
// Assumes current activity is the searchable activity
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
mSearchView.setIconifiedByDefault(true);
我使用 ActionBarCompat(带有 minSdkVersion 14 和 targetSdkVersion 19)。
我在网上搜索,似乎没有人有这种奇怪的行为......请帮助我了解问题所在。
提前致谢:)
我对与searchSuggestThreshold相关的行为产生了误解:
对每个类型字符调用 SuggestionContentProvider 是正常的,但在达到阈值之前,uri 参数不包含查询。
因此,应跳过与建议生成相关的代码。