我一直在使用SearchView
作为ActionView
中的ActionBar
小部件的应用程序中工作。
当我键入搜索并点击搜索按钮时会出现问题,它会打开相同的活动,我想要做的是打开一个新的Activity并在ListView上显示结果,如何解决?
这是我的AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="andres.hotelsoria" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher_hotel"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<activity
android:name=".SearchableActivity"
android:label="@string/title_activity_searchable" >
</activity>
</application>
答案 0 :(得分:3)
您可以通过将OnQueryTextListener
附加到SearchView来开始新活动。
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Intent intent = new Intent(getApplicationContext(), SearchableActivity.Class);
startActivity(intent);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
答案 1 :(得分:1)
阅读这篇文章 http://developer.android.com/guide/topics/search/index.html
如本文档中所述,请按照以下步骤操作
1)在res-&gt; xml-&gt; searchable.xml粘贴内容中创建一个文件夹,如文档中所述
2)转到AndroidManifest.xml并将Activity(您要将搜索结果传递到此处)更改为
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
3)将menu.xml文件中的SearchView声明为
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="your activity context" >
<item
android:id="@+id/mi_search"
android:title="@string/search"
android:orderInCategory="2"
android:icon="@drawable/searchicon"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
4)在onCreateOptionsMenu(菜单菜单)中执行此代码
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget;
5)在onOptionsItemSelected中。 - &gt;获取搜索视图的ID - &gt;在块
中简单粘贴onSearchRequested()public boolean onSearchRequested() {
return super.onSearchRequested();
}
6)使用onQueryTextListener注册searchView并执行您想要执行的操作 http://developer.android.com/reference/android/widget/SearchView.OnQueryTextListener.html
答案 2 :(得分:0)
//你要开始这样的新活动 SearchView.OnQueryTextListener textListener = new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query) {
//use intent here to start new activity and pass "query" string.
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
};
searchView.setOnQueryTextListener(textListener);