我正在尝试使用操作栏中的搜索栏小部件在我的 android 应用程序上实现搜索。
我正在关注
http://developer.android.com/guide/topics/search/search-dialog.html http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
教程来完成这项工作。
我有两项涉及搜索的活动。SearchBar 活动具有操作栏,AcceptSearch 是我的可搜索活动。即使我声明了哪个活动是可搜索的活动,查询也不会发送到 AcceptSearch 并且活动也没有启动。
我配置了搜索栏,以便在小部件为空时出现搜索栏提示,但提示也永远不会出现。这是我的代码。
搜索栏
public class SearchBar extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search_bar);
}
@Override
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();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
接受搜索
public class AcceptSearch extends ListActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
//Get the intent, verify the action and get the query
Intent intent = getIntent();
if(Intent.ACTION_SEARCH.equals(intent.getAction()))
{
String query = intent.getStringExtra(SearchManager.QUERY);
//Start the search
doMySearch();
}
}
可搜索的.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint">
</searchable>
选项菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:title="Help"
android:id="@+id/menu_help"
android:showAsAction="always"
/>
<item
android:title="Categories"
android:id="@+id/menu_cats"
android:showAsAction="always"
/>
<item
android:id="@+id/menu_search"
android:title="Search with Searchlet"
android:icon="@drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.widget.SearchView"/>
</menu>
显现
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.searchlet"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashScreenActivity"
android:label="@string/title_activity_splash_screen"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AcceptSearch">
<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=".SearchBar"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.searchlet.CLEARSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
我相信这就是重现这种情况所需的所有信息。
谢谢大家。我很欣赏。