如果你要在你的应用程序中实现搜索功能,android中为用户提供两种搜索的特性:
一种是search dialog,另一种是search widget.
由于search widget要在3.0以上的版本才能使用。这里只讲search dialog
search dialog是由android系统控制的。需要由用户去激活它。并且搜索框只出现在activity的最顶部。当提交查询的数据时,系统会转发给一个activity进行处理。用户也可以保存最近查询的数据。这里讲一下基本的配置。
1.新建一个位于res/xml下的一个searchable.xml的配置文件
<?xml version="1.0" encoding="utf-8"?><searchable xmlns:android="http://schemas.android.com/apk/res/android"<!-- label为搜索框上方的文本,hint搜索框里面的提示文本  -->android:label="@string/search_label"android:hint="@string/search_hint"android:icon="@drawable/search32"android:searchMode="showSearchLabelAsBadge"<!-- 中间是语音搜索配置(看不懂....) -->android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"android:voiceLanguageModel="free_form"android:voicePromptText="Invoke Search"<!-- 这里的值必须SearchSuggestionSampleProvider.AUTHORITY相同,后面讲 -->android:searchSuggestAuthority="SuggestionProvider"android:searchSuggestSelection=" ? ">    </searchable>


2.新建一个activity:SearchInvoke.java。此activity的作用是用来启动search dialog并把要查询的数据进行转发给另一个activity进行处理。
关键代码:
public class SearchInvoke extends Activity{private Button mStartSearch;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.search_invoke);//就一个按钮mStartSearch = (Button)findViewById(R.id.btn_start_search);//启动搜索框mStartSearch.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {onSearchRequested();}});}        //重写onSearchRequested方法@Overridepublic boolean onSearchRequested() {               //除了输入查询的值,还可额外绑定一些数据Bundle appSearchData = new Bundle();appSearchData.putString("demo_key","text");startSearch(null, false, appSearchData, false);                 //必须返回true。否则绑定的数据作废return true;}}


3.处理activity--》SearchQueryResults.java
public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.search_query_results);        //两个TextView进行数据显示        mQueryText = (TextView) findViewById(R.id.txt_query);        mAppDataText = (TextView) findViewById(R.id.txt_appdata);        final Intent queryIntent = getIntent();        final String queryAction = queryIntent.getAction();        if (Intent.ACTION_SEARCH.equals(queryAction)) {            doSearchQuery(queryIntent, "onCreate()");        }        else {            mDeliveredByText.setText("onCreate(), but no ACTION_SEARCH intent");        }    }    /**      * Called when new intent is delivered.       这个方法不知道何时调用。看了文档说在manifest中配置此activity的启动模式为singleTop。不过试了貌似还没有执行到此方法     */    @Override    public void onNewIntent(final Intent newIntent) {        super.onNewIntent(newIntent);        Log.i(TAG, "SearchQueryResults-->onNewIntent()");        // get and process search query here        final Intent queryIntent = getIntent();        final String queryAction = queryIntent.getAction();        if (Intent.ACTION_SEARCH.equals(queryAction)) {            doSearchQuery(queryIntent, "onNewIntent()");        }        else {            mDeliveredByText.setText("onNewIntent(), but no ACTION_SEARCH intent");        }    }    //处理    private void doSearchQuery(final Intent queryIntent, final String entryPoint) {        //获取查询的值          final String queryString = queryIntent.getStringExtra(SearchManager.QUERY);        mQueryText.setText(queryString);        //保存查询的值,这样在下次搜索的时候会有以前搜索数据的提示        SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,                 SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);        suggestions.saveRecentQuery(queryString, null);                //获得额外递送过来的值        final Bundle appData = queryIntent.getBundleExtra(SearchManager.APP_DATA);        if (appData == null) {            mAppDataText.setText("<no app data bundle>");        }        if (appData != null) {            String testStr = appData.getString("demo_key");            mAppDataText.setText((testStr == null) ? "<no app data>" : testStr);        }    }


3.创建类SearchSuggestionSampleProvider.java用户查询数据保存的配置信息
public class SearchSuggestionSampleProvider extends        SearchRecentSuggestionsProvider {    final static String AUTHORITY="SuggestionProvider";    final static int MODE=DATABASE_MODE_QUERIES;        public SearchSuggestionSampleProvider(){        super();        setupSuggestions(AUTHORITY, MODE);    }}


4.最重要的一步,在AndroidManifest.xml中的配置
<!-- search ui --> <activity android:name="com.zyz.app.SearchQueryResults"                  android:label="处理查询结果">            <intent-filter>                <action android:name="android.intent.action.SEARCH" />            </intent-filter>            <meta-data android:name="android.app.searchable"                       android:resource="@xml/searchable" />        </activity>        <!--此activity用来输入并递送结果,meta-data必须配置-->        <activity android:name="com.zyz.app.SearchInvoke">        <intent-filter>        <action android:name="android.intent.action.MAIN"/>        <category android:name="android.intent.category.LAUNCHER"/>        </intent-filter>        <meta-data android:name="android.app.default_searchable"        android:value="com.zyz.app.SearchQueryResults"/>        </activity>       <!--provider的注册-->        <provider android:name="com.zyz.app.SearchSuggestionSampleProvider"                  android:authorities="SuggestionProvider" />


5.无图无真相


更多相关文章

  1. 浅谈android的am命令
  2. 2011.07.20(2)——— android 计算当前坐标是否在view内
  3. Android软键盘弹出时的界面控制方法
  4. Android(安卓)binder学习一:主要概念
  5. Audio and Video
  6. android sqlite批量插入数据速度慢解决方案
  7. 利用monkey测试android,入门级用户可能遇见的错误及解决办法
  8. Android(安卓)task和affinity讲解
  9. android缩放动画的两种实现方法

随机推荐

  1. 判断scrollview中某子控件是否可见
  2. android studio gradle依赖库使用国内资
  3. Android集成React Native实现多Tab页
  4. Android标题头滑动渐变,Titlebar滑动渐变,
  5. ANDROID 8.1 多个apk同时录音
  6. Android解析自定义xml文件(方案一)
  7. failed to set system property
  8. Android 如何获取RadioGroup选中RadioBut
  9. Android: JNI WARNING: illegal start by
  10. Android TabHost的用法