请不要标记为重复,因为我已经查看了其他几个相同的问题,这些解决方案对我不起作用
category_menu.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/category.search"
android:title="Search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
Mainfest.xml
<activity android:name=".CategoryActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity" />
</intent-filter>
</activity>
<activity
android:name=".SearchResultsActivity"
android:label="@string/title_activity_search_results"
android:theme="@style/AppTheme.NoActionBar"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".CategoryActivity" />
</activity>
类别活动.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.category_menu, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.category_search).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
return true;
}
搜索结果活动.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"Search Began");
setContentView(R.layout.search_results_activity);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
Log.d(TAG, "Herer");
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
Log.d(TAG, query);
}
}
res/xml/下的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="Recipe Search" />
您应该在searchView中添加onQueryTextListener
。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.category_menu, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.category_search).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
searchView..setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
startActivity(CategoryActivity.this, SearchResultActivity.class);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return true;
}
android提示和标签的类型都必须为"@String/…"硬编码文本将阻止SearchResultActivity启动。
如果searchable.xml
中不包含android:label
,则不会启动SearchResultActivity
,请确保在searchable.xml
:中添加此代码
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name" ===================> can not contain empty strings
android:hint="Recipe Search" />