我正在尝试使用搜索栏中的搜索视图添加搜索活动。我看过几个这样的问题,但是我尝试了所有答案,对我没有任何帮助。搜索正确显示,但是当我输入搜索短语并单击搜索后,什么也不会发生。我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.y2apps.quoteformessenger.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="android.app.default_searchable" android:resource="@xml/searchable" android:value=".SearchResultsActivity"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
</activity>
<activity
android:name=".SearchResultActivity"
android:theme="@style/AppTheme"
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" />
</activity>
</application>
searachable.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" />
style.xml文件
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
主要活动
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowHomeEnabled(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
if (searchView != null) {
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
} else {
Toast.makeText(this, "can't find search bar", Toast.LENGTH_LONG).show();
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
和搜索活动
public class SearchResultActivity extends Activity {
// ===========================================================
// Public Constants
// ===========================================================
// ===========================================================
// Private constants and Fields
// ===========================================================
// ===========================================================
// Constructors
// ===========================================================
// ===========================================================
// Public methods
// ===========================================================
@Override
public void onCreate(Bundle savedInstanceState) {
handleIntent(getIntent());
}
// ===========================================================
// Private methods
// ===========================================================
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
Toast.makeText(this, "YARON YOREH 2", Toast.LENGTH_LONG).show();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Toast.makeText(this, "YARON YOREH asked for " + query, Toast.LENGTH_LONG).show();
//use the query to search your data somehow
}
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
如果有助于诊断此提示,也不会出现搜索提示。谢谢您的任何帮助
您的清单中有一个错别字:
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
我相信应该是:
<meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity"/>
但是,您不需要,因为您已经为应用程序定义了可搜索的默认可搜索
根据您的代码,我了解的是您的主要目的是搜索活动,您想在新活动中显示结果。为此,您必须将MainActivty作为可搜索。将您的清单更改为以下。 进行搜索的主要活动
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
展示结果的活动
<activity android:name=".SearchResultActivity" ... >
<meta-data android:name="android.app.default_searchable"
android:value=".MainActivity" />
</activity>