設定搜尋介面

建議您使用 SearchView 以項目形式呈現,藉此在應用程式中提供搜尋功能。阿斯 其中包含應用程式列中的所有項目,您就可以將 SearchView 定義為 或只在有會議室時顯示。您也可以將其定義為 可收合的動作,顯示 SearchView 圖示 然後將整個應用程式列做為搜尋欄位顯示 輕觸圖示。

在應用程式列中新增 SearchView

如要在應用程式列中新增 SearchView 小工具,請在 名為 res/menu/options_menu.xml 的專案,並加入下列程式碼 加入該檔案。以下程式碼會定義如何建立搜尋項目,例如圖示 項目名稱與名稱collapseActionView 屬性 可讓 SearchView 展開並顯示整個應用程式列 未使用時,收合為一般的應用程式列項目由於 手機裝置的應用程式列空間有限,建議您使用 collapsibleActionView 屬性來提供更優質的使用者 無須專人管理

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@drawable/ic_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="androidx.appcompat.widget.SearchView" />
</menu>
敬上

如要取得更易於存取的搜尋圖示,請 「/res/drawable」資料夾中有 ic_search.xml 個檔案,且 加入下列程式碼:

<vector
    android:height="24dp"
    android:tint="#000000"
    android:viewportHeight="24"
    android:viewportWidth="24"
    android:width="24dp"
    xmlns:android="http://schemas.android.com/apk/res/android">
        <path android:fillColor="@android:color/white" android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</vector>

如要在應用程式列中顯示 SearchView,請加載 XML 選單 資源 res/menu/options_menu.xml 位於 onCreateOptionsMenu() 方法是

Kotlin

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.options_menu, menu)

    return true
}

執行應用程式會產生如下的內容:

圖片:空白畫面,在應用程式頂端列中有搜尋圖示
圖 1. 應用程式頂端列中的搜尋圖示。

SearchView 會顯示在應用程式的應用程式列中,但非 以及功能正常運作輕觸搜尋圖示後,系統會顯示以下類似內容:

顯示搜尋檢視畫面實際操作的圖片
圖 2. SearchView

如要讓 SearchView 函式順利運作,必須定義 SearchView 運作。

建立搜尋設定

搜尋 設定會指定 SearchView 的行為和 res/xml/searchable.xml 檔案中定義的資源搜尋設定 必須包含的 android:label 屬性,且該屬性須 值與android:label <應用程式><activity> 元素。不過,我們也建議您新增 android:hint 屬性能讓使用者瞭解應該輸入什麼內容 。

<?xml version="1.0" encoding="utf-8"?>

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name"
        android:hint="@string/search_hint" />

在應用程式的資訊清單檔案中, <meta-data>敬上 元素,res/xml/searchable.xml宣告 加入要顯示內容的 <activity> 元素中 SearchView

<activity
android:name=".SearchResultsActivity"
android:exported="false"
android:label="@string/title_activity_search_results"
android:launchMode="singleTop"
android:theme="@style/Theme.AppCompat.Light">
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data
        android:name="android.app.searchable"
        android:resource="@xml/searchable" />
</activity>

在您建立的 onCreateOptionsMenu() 方法中,將 透過呼叫 SearchView 找到搜尋設定 setSearchableInfo(SearchableInfo):

Kotlin

override fun onCreateOptionsMenu(menu: Menu): Boolean {
    menuInflater.inflate(R.menu.options_menu, menu)

    val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
    val searchView = menu.findItem(R.id.search).actionView as SearchView
    val component = ComponentName(this, SearchResultsActivity::class.java)
    val searchableInfo = searchManager.getSearchableInfo(component)
    searchView.setSearchableInfo(searchableInfo)
    return true
}

呼叫 getSearchableInfo() 取得 SearchableInfo 物件。搜尋時 您的 SearchView 和 使用者提交查詢,SearchView 會啟動包含 ACTION_SEARCH 意圖。接著,您需要活動可以篩選這項意圖,並處理 搜尋查詢。

建立可供搜尋的活動

ACTION_SEARCH 意圖和 - 可供搜尋的活動篩選器 資料集裡的查詢如要建立可供搜尋的活動,請宣告 你選擇篩選的ACTION_SEARCH活動 意圖:

<activity android:name=".SearchResultsActivity" ... >
    ...
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    ...
</activity>

在可供搜尋的活動中,透過下列方式處理 ACTION_SEARCH 意圖: 檢查是否位於 onCreate() 方法。

Kotlin

class SearchResultsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search_results)
        handleIntent(intent)
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent) {
        if (Intent.ACTION_SEARCH == intent.action) {
            val query = intent.getStringExtra(SearchManager.QUERY)
            Log.d("SEARCH", "Search query was: $query")
        }
    }
}

現在,SearchView 可以接受使用者的查詢並啟動 使用 ACTION_SEARCH 意圖且可供搜尋的活動。

取得搜尋查詢後,您可以將其傳遞至 ViewModel,可用於 以擷取要顯示的搜尋結果