Android中Creating a Searchable Activity


1.在<intent-filter>中用ACTION SEARCH声明这个Activity。

2.用<meta-data>来指定searchable configuration。

例如:

<application ... >
    <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>
    ...
</application>

执行查询:

当你的activity启动时,通过下面的代码来获得查询:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    // Get the intent, verify the action and get the query
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      String query = intent.getStringExtra(SearchManager.QUERY);
      doMySearch(query);
    }
}

doMySearch(query)是你自己的函数,用来完成查询的具体工作。

相关内容