添加最近用户搜索选项是很好的用户体验
第一步。实现SearchRecentSuggestionsProvider的子类,并在构造方法中调用setupSuggestions方法.
public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = "com.example.MySuggestionProvider";
public final static int MODE = DATABASE_MODE_QUERIES;
public MySuggestionProvider() {
setupSuggestions(AUTHORITY, MODE);
}
}
authority 只要是一个不重复的字符串就可以,mode 一定要包含DATABASE_MODE_QUERIES,
public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;
也可以添加DATABASE_MODE_2LINES 来添加两行的输入建议.
第二步,在manifest文件中声明provide
<application>
<provider android:name=".MySuggestionProvider"
android:authorities="com.example.MySuggestionProvider" />
...
</application>
第三步,在在searchable 配置中增加searchSuggestAuthority和searchSuggestSelect
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint"
android:searchSuggestAuthority="com.example.MySuggestionProvider"
android:searchSuggestSelection=" ?" >
</searchable>
第四步,保存用户的输入.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
suggestions.saveRecentQuery(query, null);
}
}
可以采用下面的方法来清理最近的搜索
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
HelloSuggestionProvider.AUTHORITY, HelloSuggestionProvider.MODE);
suggestions.clearHistory();
第一步。实现SearchRecentSuggestionsProvider的子类,并在构造方法中调用setupSuggestions方法.
public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = "com.example.MySuggestionProvider";
public final static int MODE = DATABASE_MODE_QUERIES;
public MySuggestionProvider() {
setupSuggestions(AUTHORITY, MODE);
}
}
authority 只要是一个不重复的字符串就可以,mode 一定要包含DATABASE_MODE_QUERIES,
public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;
也可以添加DATABASE_MODE_2LINES 来添加两行的输入建议.
第二步,在manifest文件中声明provide
<application>
<provider android:name=".MySuggestionProvider"
android:authorities="com.example.MySuggestionProvider" />
...
</application>
第三步,在在searchable 配置中增加searchSuggestAuthority和searchSuggestSelect
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint"
android:searchSuggestAuthority="com.example.MySuggestionProvider"
android:searchSuggestSelection=" ?" >
</searchable>
第四步,保存用户的输入.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
suggestions.saveRecentQuery(query, null);
}
}
可以采用下面的方法来清理最近的搜索
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
HelloSuggestionProvider.AUTHORITY, HelloSuggestionProvider.MODE);
suggestions.clearHistory();