十七、Android學習筆記_Android 使用 搜尋方塊

來源:互聯網
上載者:User

標籤:android   style   class   blog   code   java   

1、在資源檔夾下建立xml檔案夾,並建立一個searchable.xml:

android:searchSuggestAuthorityshux屬性的值跟實現SearchRecentSuggestionsProvider類中的setupSuggestions方法的第一個參數相同。
android:searchSuggestSelection 指搜尋參數
<?xml version="1.0" encoding="utf-8"?><searchable xmlns:android="http://schemas.android.com/apk/res/android"    android:label="@string/app_label"    android:hint="@string/search_hint"     android:searchSuggestAuthority="com.example.search.provider.MySuggestionProvider"    android:searchSuggestSelection=" ?"></searchable>
2、設定檔 
  2.1 配置全域的搜尋方塊
  啟動的activity是SearchableActivity。分別在MainActivity和OtherActivity調用onSearchRequested()可以啟用搜尋方塊。映射是必須有"_id",
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.search"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.search.MainActivity"            android:label="@string/app_name"            android:launchMode="singleTop" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>               <!-- 放在外面就是全域 -->        <meta-data                android:name="android.app.default_searchable"                android:value=".SearchableActivity" />        <!-- 點擊搜尋結果要跳轉到的activity -->        <activity android:name=".SearchableActivity" >            <intent-filter>                <action android:name="android.intent.action.SEARCH" />            </intent-filter>            <meta-data                android:name="android.app.searchable"                android:resource="@xml/searchable" />        </activity>        <activity android:name=".OtherActivity"></activity>        <provider            android:name="com.example.search.provider.MySuggestionProvider"            android:authorities="com.example.search.provider.MySuggestionProvider" />    </application></manifest>

  2.2 為某一個Activity配置搜尋方塊

      為MainActivity配置了一個啟用SearchableActivity的搜尋方塊。

        <activity            android:name="com.example.search.MainActivity"            android:label="@string/app_name"            android:launchMode="singleTop" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>            <!-- 在某個activity的內部,表示當前的activity可以調出搜尋方塊, 指定要啟用的 SearchableActivity -->            <meta-data                android:name="android.app.default_searchable"                android:value=".SearchableActivity" />        </activity>                   <!-- 點擊搜尋結果要跳轉到的activity -->        <activity android:name=".SearchableActivity" >            <intent-filter>                <action android:name="android.intent.action.SEARCH" />            </intent-filter>            <meta-data                android:name="android.app.searchable"                android:resource="@xml/searchable" />        </activity>

      2.3 搜尋之後,停留在當前Activity。

  如果停留在當前Activity,需要設定 launchMode="singleTop",並且在當前的Activity加入以下代碼,還需要在onCreate方法裡面調用handleIntent(intent)方法。

    @Override    protected void onNewIntent(Intent intent) {        setIntent(intent);        handleIntent(intent);    }    private void handleIntent(Intent intent) {if (Intent.ACTION_SEARCH.equals(intent.getAction())) {          String query = intent.getStringExtra(SearchManager.QUERY);          doMySearch(query);        }    }    private void doMySearch(String query) {        Toast.makeText(this, "res: "+query, Toast.LENGTH_SHORT).show();    }

  設定檔

      <activity            android:name="com.example.search.MainActivity"            android:label="@string/app_name"            android:launchMode="singleTop" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>                       <intent-filter>                <action android:name="android.intent.action.SEARCH" />            </intent-filter>            <meta-data                android:name="android.app.searchable"                android:resource="@xml/searchable" />        </activity>

3、建立provider

  需要繼承SearchRecentSuggestionsProvider類,重寫query方法,需要將查詢出來的資料轉化成MatrixCursor對象然後返回。為了進一步處理,需要將當前點擊的項的參數通過SearchManager.SUGGEST_COLUMN_QUERY傳過去,在activity接收時intent.getStringExtra(SearchManager.QUERY),在跳轉的activity中,就可以繼續進行操作。

public class MySuggestionProvider extends SearchRecentSuggestionsProvider {    // AUTHORITY:它的範圍searchable.xml中的searchSuggestAuthority一樣    public final static String AUTHORITY = "com.example.search.provider.MySuggestionProvider";    public final static int MODE = DATABASE_MODE_QUERIES;    public MySuggestionProvider() {        setupSuggestions(AUTHORITY, MODE);    }    @Override    public Cursor query(Uri uri, String[] projection, String selection,            String[] selectionArgs, String sortOrder) {        // 在搜尋方塊中輸入的值        String query = selectionArgs[0];                Log.i("tag", query);        Log.i("tag", uri.getLastPathSegment().toLowerCase());        return getSuggestions(query);    }    private Cursor getSuggestions(String query) {        String processedQuery = query == null ? "" : query.toLowerCase();        List<Person> persons = DataSource.getInstance().getPersons(processedQuery);        MatrixCursor cursor = new MatrixCursor(COLUMNS);        long id = 0;        for (Person person : persons) {            cursor.addRow(columnValuesOfWord(id++, person));        }        return cursor;    }    private Object[] columnValuesOfWord(long id, Person person) {        return new Object[] { id, // _id                person.name, // text1                person.id, // text2                person.name        };    }    private static final String[] COLUMNS = { "_id",            SearchManager.SUGGEST_COLUMN_TEXT_1,            SearchManager.SUGGEST_COLUMN_TEXT_2,            SearchManager.SUGGEST_COLUMN_QUERY//            SearchManager.SUGGEST_COLUMN_INTENT_DATA,// 資料傳遞到intenter中    };}

 

http://www.cnblogs.com/zhengbeibei/archive/2013/01/17/2865610.html

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.