我尝试使用搜索栏中的搜索视图添加搜索活动。我已经看过这样的一些问题,但我已经尝试了所有的答案,对我来说没有任何作用。搜索显示正常,但当我输入搜索短语并单击搜索后,没有任何反应。 我的清单
<?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" />
styles.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
// ===========================================================
}
如果有助于诊断,搜索提示也不会出现。 谢谢你的帮助
答案 0 :(得分:0)
你的清单上有拼写错误:
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
我认为应该是:
<meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity"/>
但是,您不应该需要,因为您已经为应用程序全局定义了默认的可搜索范围
答案 1 :(得分:0)
根据您的代码,我理解的是您的MainActivity是搜索活动,您希望在新活动中显示结果。为此,您必须将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>