Android 搜索视图未调用搜索 activity
Android Search view not invoking search activity
Please dont mark as duplicate as I have viewed several other same question and those solution didnt worked for me.
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>
CategoryActivity.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;
}
SearchResultsActivity.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);
}
}
Searchable.xml 在 res/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;
}
如果 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" />
android 提示和标签必须都是“@String/...”类型
硬编码文本将阻止 SearchResultActivity 启动。
Please dont mark as duplicate as I have viewed several other same question and those solution didnt worked for me.
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>
CategoryActivity.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;
}
SearchResultsActivity.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);
}
}
Searchable.xml 在 res/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;
}
如果 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" />
android 提示和标签必须都是“@String/...”类型 硬编码文本将阻止 SearchResultActivity 启动。