Android4.0之后,Android内置了一个搜索控件,配合ActionBar上面的搜索按钮,相当不错好看,这次使用了下,觉得很不错。

这个搜索的好处在于你点击后,他会自动弹出个搜索框,输入内容后会自动弹出匹配的内容,形成一个列表,选择后会弹到你想要去的界面。

类似这样的



你需要在代码中的onCreateOptionsMenu中加入

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);


然后在onOptionsItemSelected中加入

case R.id.search:
onSearchRequested();
return true;

这样才会执行搜索功能。


从开始看代码

handleIntent(getIntent());这个方法在onCreate中就执行了

并且在
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}

也执行了,说明这个方法比算较重要,其实这个是一个搜索按钮用的因为在android4.0以上系统 输入法里有个“搜索”按钮或者是“前往”按钮

这样的搜索按钮,这个handleIntent(getIntent())主要就是执行的这个搜索

看里面的代码:

private void handleIntent(Intent intent) {
//Intent.ACTION_VIEW android:searchSuggestIntentAction="android.intent.action.VIEW" 还在这个view中
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
// handles a click on a search suggestion; launches activity to show word
Intent wordIntent = new Intent(this, WordActivity.class);
wordIntent.setData(intent.getData());
startActivity(wordIntent);
finish();

} else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// handles a search query Intent.ACTION_SEARCH这个Intent是在当我点击系统的搜索框右面的按钮时触发的
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}

上面就是handleIntent里面的代码

handleIntent里的代码有两个意思 一个就是当Intent是Intent.ACTION_VIEW.这个的时候跳到你指定的类中必然WordActivity,其实这个ntent.ACTION_VIEW就是你在搜索出建议后点击建议里面的列表进入的界面,比如你点击这个就执行的是这个intent,执行这个intent,会返回一个URI,可以使用intent.getData()这个方法得到这个返回的URI,这个URI是系统自己返回的。可以看配置文件searchable.xml这个文件,这个文件的代码如下:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search_label"
android:hint="@string/search_hint"
android:searchSettingsDescription="@string/settings_description"
android:searchSuggestAuthority="com.example.android.searchabledict.DictionaryProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData="content://com.example.android.searchabledict.DictionaryProvider/dictionary/sugger_que54r"
android:searchSuggestSelection=" ?"
android:searchSuggestThreshold="1"
android:includeInGlobalSearch="true"
>
</searchable>


就是一个XML文件,里面配置的是android:searchSuggestAuthority这个是在AndroidManifest.xml里面配置的provider,比如

<provider android:name=".DictionaryProvider"
android:authorities="com.example.android.searchabledict.DictionaryProvider" />

里面的android:searchSuggestIntentData配置的就是你要返回的intentData,比如这里面配的是content://com.example.android.searchabledict.DictionaryProvider/dictionary/sugger_que54r到时候你在

if (Intent.ACTION_VIEW.equals(intent.getAction())) {
// handles a click on a search suggestion; launches activity to show word
Intent wordIntent = new Intent(this, WordActivity.class);
wordIntent.setData(intent.getData());
startActivity(wordIntent);
finish();

} 这个里面的intent.getData()就会是content://com.example.android.searchabledict.DictionaryProvider/dictionary/sugger_que54r/(你点击的那个Item的ID)也就是一个带ID的URI,至于这里的ID,是你在代码里赋值给他的 ,这个后面说。

当如果是你点击右下角的搜索按钮的时候执行的是ACTION_SEARCH这个intent,会执行showResults这个方法。

private void showResults(String query) {


Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, selection,
new String[]{query}, null);


if (cursor == null) {
// There are no results
mTextView.setText(getString(R.string.no_results, new Object[] {query}));
} else {
// Display the number of results
int count = cursor.getCount();
String countString = getResources().getQuantityString(R.plurals.search_results,
count, new Object[] {count, query});
mTextView.setText(countString);


// Specify the columns we want to display in the result
String[] from = new String[] { DictionaryDatabase.KEY_WORD,
DictionaryDatabase.KEY_DEFINITION };


// Specify the corresponding layout elements where we want the columns to go
int[] to = new int[] { R.id.word,
R.id.definition };


// Create a simple cursor adapter for the definitions and apply them to the ListView
SimpleCursorAdapter words = new SimpleCursorAdapter(this,
R.layout.result, cursor, from, to);
mListView.setAdapter(words);


// Define the on-click listener for the list items
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Build the Intent used to open WordActivity with a specific word Uri
Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class);
Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
String.valueOf(id));
wordIntent.setData(data);
startActivity(wordIntent);
}
});
}
}

以上是showResult这个方法里穿进去的query这个字符串就是你搜索时候输入的字符串。managedQuery就是你自己想怎么搜索自己定义的搜索语句,然后搜索出结果。

接下来解释下String countString = getResources().getQuantityString(R.plurals.search_results,
count, new Object[] {count, query});

这段代码。这段代码只要是plurals,plurals是一个复数,是在String.xml文件中配置的,配置文件如下:

<plurals name="search_results">
<item quantity="one">%1$d result for \"%2$s\": </item>
<item quantity="other">%1$d results for \"%2$s\": </item>
</plurals>

plurals的负数的意思是,如果要是查出的结果是一个也就是quantity="one",则提示%1$d result for \"%2$s\"这里的%1$d和\"%2$s\"都是类似占位符,取的是前面new Object[] {count, query}传进来的值,这里主要是区分一个和多个,不如一个时候提示你搜索的结果是1个,多个的时候提示你搜索的结果有多个,可能英语中result和results这种复数的不同导致他们这样设计,你也可以设置 <item quantity="two">这种根据自己的定义提示。

接下来就进入一个列表,然后点击列表中的数据,会执行你自己定义的方法,需要提示的是item的ID是你传值进去的


我们来看provider中,provider中buildUriMatcher这个方法主要是根据你的URI匹配来进行的,比如matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);就会返回一个SEARCH_SUGGEST这样的int值,

这个方法主要是在query这个方法中做判断:

如下:

switch (sURIMatcher.match(uri)) {
case SEARCH_SUGGEST:
if (selectionArgs == null) {
throw new IllegalArgumentException(
"selectionArgs must be provided for the Uri: " + uri);
}
return getSuggestions(selectionArgs[0]);
case SEARCH_WORDS:
if (selectionArgs == null) {
throw new IllegalArgumentException(
"selectionArgs must be provided for the Uri: " + uri);
}
return search(selectionArgs[0]);

这样就会执行你的方法,这些方法,都是你输入关键字时候系统自动搜索的,你需要把你的自动赋值给系统定义好的字段,查询语句类似如下:

String[] columns = new String[] {
BaseColumns._ID,
BaseColumns._ID+" AS "+SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID,
CorpInfoColumns.NAME+" AS "+SearchManager.SUGGEST_COLUMN_TEXT_1,
CorpInfoColumns.LEGAL_PERSON+" AS "+SearchManager.SUGGEST_COLUMN_TEXT_2

};
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
String selection = CorpInfoColumns.NAME + " like ? OR "+CorpInfoColumns.LEGAL_PERSON+ " like ? ";
String[] selectionArgs = new String[] {"%"+query+"%","%"+query+"%"};、

你需要把你自己的_id的行名必须为给SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID,这样前面才会返回一个带ID的URI,把你搜索的第一行命名给SearchManager.SUGGEST_COLUMN_TEXT_1,第二行的行名必须为SearchManager.SUGGEST_COLUMN_TEXT_2,这样系统才会自动显示。否则会显示一片白色。

这个adapter其实应该可以自己定义的,只不过我没有定义过,有兴趣的可以试试。


欢迎程序员加群 169146045 扯淡 聊天 讨论技术






更多相关文章

  1. 【Android 界面效果29】研究一下Android滑屏的功能的原理,及scrol
  2. Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
  3. Android中图片实现按钮点击效果
  4. android 通过lint以及android-resource-remover清楚不用的资源以
  5. android Shape Drawable创建两边半圆的按钮背景
  6. Android 获取无线蓝牙MAC信息代码
  7. Android 三个按钮对话框
  8. Android 照相机打开方法
  9. android 简单拨号器 代码

随机推荐

  1. 由浅入深玩转华为WLAN—29 高可用性系列(5
  2. 用MDT 2012为企业部署windows 7[收藏笔记
  3. 内容观察者广播
  4. 内容提供者简介
  5. Mybaits的核心配置
  6. 安卓数据库案例
  7. Android 开发后台
  8. 安卓数据操作
  9. PPT插件OKplus软件和学习视频下载
  10. 消息处理