日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

android中的search dialog

系統 3100 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條評論
主站蜘蛛池模板: 鄄城县| 棋牌| 米泉市| 温宿县| 马尔康县| 榆中县| 金塔县| 香港 | 水富县| 合肥市| 会昌县| 历史| 独山县| 永清县| 聂荣县| 定襄县| 徐州市| 凉山| 姚安县| 平塘县| 大埔区| 墨江| 泾源县| 苏州市| 望奎县| 东源县| 汨罗市| 马鞍山市| 建瓯市| 蚌埠市| 西藏| 富宁县| 伊川县| 汝南县| 洛阳市| 长白| 修武县| 临漳县| 罗江县| 崇明县| 恩平市|