-1
在我發佈之前,我嘗試了所有可能的解決方案。最後我陷入了困境,沒有運氣。我只想在搜索活動中顯示查詢字符串。任何幫助?提前致謝。從未調用過Android搜索活動
的AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/my_app"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="android.app.default_searchable"
android:value="com.mycompany.mygoodapp.SearchResultsActivity"/>
<activity
android:name=".MainActivity"
android:configChanges="orientation"
android:label="@string/my_app"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchResultsActivity"
android:label="@string/my_app"
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>
SearchResultsActivity實現如下:
public class SearchResultsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
DisplayToast("Here ...");
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
DisplayToast(query);
}
}
public void DisplayToast(String s) {
Toast.makeText(SearchResultsActivity.this,
s, Toast.LENGTH_LONG).show();
}
}
searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/my_app"
android:hint="@string/search_hint" />
MainActivity:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
ComponentName cn = new ComponentName("com.mycompany.mygoodapp", "com.mycompany.mygoodapp.SearchResultsActivity");
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
searchView.setIconifiedByDefault(true);
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
///
else if(id==R.id.search)
{
onSearchRequested();
return true;
}
return super.onOptionsItemSelected(item);
}
主菜單佈局搜索菜單項:
<item
android:id="@+id/search"
android:title="@string/search_title"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView">
「我嘗試了所有可能的解決辦法張貼在這裏」你嘗試過哪些解決方案? –
嗨dask。只需查看本教程http://blog.nkdroidsolutions.com/android-searchview-in-listview-example-tutorial/並自己嘗試一下。 – RameshJaga
@dask檢查我的答案 –