我試圖在搜索欄中使用搜索視圖添加搜索活動。我見過這樣的幾個問題,但我已經嘗試了所有的答案,沒有爲我工作。搜索顯示正確,但當我輸入搜索短語並點擊搜索後,什麼也沒有發生。 我的清單SearchView未啓動搜索活動
<?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
// ===========================================================
}
如果它可以幫助診斷這一點,搜索提示沒有顯示出來無論是。 感謝您的幫助
謝謝你馬克。這解決了問題 – yaronbic