android中的search dialog

系统 2279 0
如果你要在你的应用程序中实现搜索功能,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;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_invoke);
		//就一个按钮
		mStartSearch = (Button)findViewById(R.id.btn_start_search);
		//启动搜索框
		mStartSearch.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onSearchRequested();
			}
		});
	}

        //重写onSearchRequested方法
	@Override
	public 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.无图无真相

android中的search dialog

android中的search dialog


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论