android內建搜尋對話方塊(浮動搜尋)例子

來源:互聯網
上載者:User

先上:

步驟:

(1)配置search bar的相關資訊,建立一個位於res/xml下的一個searchable.xml的設定檔

<?xml version="1.0" encoding="utf-8"?><searchable  xmlns:android="http://schemas.android.com/apk/res/android"  android:hint="@string/searchHint"   android:searchMode="showSearchLabelAsBadge"    android:searchSuggestAuthority="com.android.cbin.SearchSuggestionSampleProvider"    android:searchSuggestSelection=" ? ">  </searchable>

(2) manifest.xml配置,搜尋結果處理的Activity將出現兩種情況,一種是從其他Activity中的search bar開啟一個Activtiy

專門處理搜尋結果,第二種是就在當前Activity就是處理結果的Activity,這配置裡包含兩種情況,自己可以看代碼能分辨出來。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.android.cbin"      android:versionCode="1"      android:versionName="1.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".Main"                  android:label="@string/app_name">            <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=".SearchResultActivity" />        </activity>    <activity android:name="SearchResultActivity" android:launchMode="singleTop">            <intent-filter>            <action android:name="android.intent.action.SEARCH"></action>        </intent-filter>        <meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data>    </activity><provider android:name="SearchSuggestionSampleProvider" android:authorities="com.android.cbin.SearchSuggestionSampleProvider"></provider></application>    <uses-sdk android:minSdkVersion="7" /></manifest> 
(3 )  儲存記錄  上面authorities指向的都是name中所關聯的SearchSuggestionSampleProvider,他是一個SearchRecentSuggestionsProvider的子類
package com.android.search;import android.content.SearchRecentSuggestionsProvider;public class SearchSuggestionSampleProvider extends        SearchRecentSuggestionsProvider {    final static String AUTHORITY="com.android.search.SearchSuggestionSampleProvider";    final static int MODE=DATABASE_MODE_QUERIES;        public SearchSuggestionSampleProvider(){        super();        setupSuggestions(AUTHORITY, MODE);    }}

(4)為了能夠使用search bar 我們必須重寫Activity的onSearchRequested的方法,在介面上啟動一個search bar

但是這個動作不會自動觸發,必須通過一個按鈕或者菜單的點擊事件觸發;

package com.android.search;import com.android.search.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class Main extends Activity implements OnClickListener{    /** Called when the activity is first created. */    private EditText etdata;    private Button btnsearch;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                findview();    }        private void findview(){        etdata=(EditText)findViewById(R.id.etdata);        btnsearch=(Button)findViewById(R.id.btncall);        btnsearch.setOnClickListener(this);    }    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        onSearchRequested();    }        @Override    public boolean onSearchRequested(){                String text=etdata.getText().toString();        Bundle bundle=new Bundle();        bundle.putString("data", text);                //開啟浮動搜尋方塊(第一個參數預設添加到搜尋方塊的值)        //bundle為傳遞的資料        startSearch("哈哈", false, bundle, false);        //這個地方一定要返回真 如果只是super.onSearchRequested方法        //不但onSearchRequested(搜尋方塊預設值)無法添加到搜尋方塊中        //bundle也無法傳遞出去        return true;    }    }

(5) 在本Activity中搜尋

package com.android.search;import com.android.search.R;import android.app.Activity;import android.app.SearchManager;import android.content.Intent;import android.os.Bundle;import android.provider.SearchRecentSuggestions;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class SearchResultActivity extends Activity implements OnClickListener{    private TextView tvquery,tvdata;    private Button btnsearch;    @Override    protected void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.searchresult);                tvquery=(TextView)findViewById(R.id.tvquery);        tvdata=(TextView)findViewById(R.id.tvdata);        btnsearch=(Button)findViewById(R.id.btnSearch);        doSearchQuery();                btnsearch.setOnClickListener(this);    }        public void doSearchQuery(){        final Intent intent = getIntent();        //獲得搜尋方塊裡值        String query=intent.getStringExtra(SearchManager.QUERY);        tvquery.setText(query);        //儲存搜尋記錄        SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,                SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);        suggestions.saveRecentQuery(query, null);        if(Intent.ACTION_SEARCH.equals(intent.getAction())){            //擷取傳遞的資料            Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);            if(bundled!=null){                String ttdata=bundled.getString("data");                tvdata.setText(ttdata);            }else{                tvdata.setText("no data");            }        }    }    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        onSearchRequested();    }        @Override    public boolean onSearchRequested(){                startSearch("onNewIntent", false, null, false);        return true;    }        @Override    public void onNewIntent(Intent intent){        super.onNewIntent(intent);        //獲得搜尋方塊裡值        String query=intent.getStringExtra(SearchManager.QUERY);        tvquery.setText(query);        //儲存搜尋記錄        SearchRecentSuggestions suggestions=new SearchRecentSuggestions(this,                SearchSuggestionSampleProvider.AUTHORITY, SearchSuggestionSampleProvider.MODE);        suggestions.saveRecentQuery(query, null);        if(Intent.ACTION_SEARCH.equals(intent.getAction())){            //擷取傳遞的資料            Bundle bundled=intent.getBundleExtra(SearchManager.APP_DATA);            if(bundled!=null){                String ttdata=bundled.getString("data");                tvdata.setText(ttdata);            }else{                tvdata.setText("no data");            }        }    }}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.