收到我實現一個活動,我想添加一個搜索欄。我希望當前的活動能夠收回意圖。我跟着可用的幾個例子,在我的應用程序添加下面的代碼:搜索意圖不是當前活動
AndroidManifest.xml中
<activity
android:name=".search.BrowseCategory"
android:launchMode="singleTop"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
我加在AndroidManifest.xml以下下的應用程序標籤:
<meta-data
android:name="android.app.default_searchable"
android:value=".search.BrowseCategory" />
現在我的BrowseCategory活動:
@Override
protected void onNewIntent(Intent intent) {
Log.d(getPackageName(), "Search intent received!");
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
// Do work using string
doSearch(query);
}
}
private void doSearch(String query) {...}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_filter, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.search:
startSearch("", false, null, false);
break;
}
return true;
}
搜索菜單
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="ifRoom"
android:title="@string/search"/>
</menu>
檢索XML
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search"
android:hint="@string/search" >
</searchable>
有了這個,我得到我的動作條搜索字段。但是,我在BrowseCategory活動中的onNewIntent沒有通過按軟鍵盤上的輸入或搜索圖標獲得呼叫。有人可以幫助我解決這個問題嗎?我在這裏錯過了什麼?非常感謝!
是否使用'SearchView'? – Gattsu 2014-09-30 06:51:38
是的,在動作條 Android上搜索查看窗口小部件:actionViewClass =「android.widget.SearchView」 – Keya 2014-09-30 06:56:31
你基本上要接收的意圖在同一個活動? – 2014-09-30 07:00:31