按下回车时未启动搜索活动。搜索视图很好地显示在操作栏上。但是当我输入搜索查询并按回车时
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.punit.rateit"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="com.punit.rateit.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:theme="@style/Theme.AppCompat.Light"
android:name=".SearchPageActivity">
<meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity" />
<intent-filter>
<action android:name="android.intent.action.SearchPage" />
</intent-filter>
</activity>
<activity android:name="com.punit.rateit.SearchResultsActivity" android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</intent-filter>
</activity>
</application>
</manifest>
这是 Menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:com.punit.rateit="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="@+id/search"
android:icon="@drawable/ic_search"
android:title="@string/action_search"
com.punit.rateit:actionViewClass="android.widget.SearchView"
com.punit.rateit:showAsAction="ifRoom"
/>
</menu>
显示操作栏的活动。
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu);
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setSubmitButtonEnabled (true);
return super.onCreateOptionsMenu(menu);
}
SearchResult 活动应该是按下搜索提交按钮时调用的活动
public class SearchResultsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("search", "search triggered");
setContentView(R.layout.searchpage);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
Log.d("search", "search triggered");
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent)
{
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.d("search", query);
}
}
@Override
public boolean onSearchRequested() {
Log.d("search", "search triggered");
return false; // don't go ahead and show the search box
}