我想在我的SearchView
中添加ActionBar
,所以我在Google Tutorial报告中here完成了这项工作。
的AndroidManifest.xml
<activity
android:name="it.polimi.dima.sound4u.activity.SoundSearchActivity"
android:label="@string/title_activity_sound_search" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<activity
android:name="it.polimi.dima.sound4u.activity.MyGiftsActivity"
android:label="@string/title_activity_my_gifts" >
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
searchable.xml
<?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" />
MyGiftsActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_gifts, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
case R.id.action_logout:
doLogout();
return true;
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
my_gifts.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search"
android:title="@string/search_title"
android:icon="@drawable/ic_action_search"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
<item android:id="@+id/action_logout"
android:title="@string/logout_label"
android:showAsAction="ifRoom" />
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
SoundSearchActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sound_search);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.w(TAG_LOG, query);
}
}
SearchView
已正确创建,可以从Icon
模式切换到TextView
模式,但当我按下键盘上的Search Button
新SoundSearchActivity
时}没有开始。有人可以帮帮我吗?
答案 0 :(得分:2)
在你的清单中试试这个:
将launchMode =“singleTop”添加到SearchActivity,以确保堆栈顶部只有一个实例。新的搜索意图将在你已经实现的新内容中出现。
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name="it.polimi.dima.sound4u.activity.MyGiftsActivity"
android:label="@string/title_activity_my_gifts" >
<meta-data android:name="android.app.default_searchable"
android:value="it.polimi.dima.sound4u.activity.SoundSearchActivity"/>
Google的初步指南(找到here)也没有给我一个正常工作的SearchView,但是做了这些更改。