我正在尝试使用操作栏中的搜索栏小部件在我的
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; }
AcceptSearch
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(); } }
Searchable.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>
options_menu
<?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>
我相信所有需要的信息来重现这种情况.
谢谢大家.我很感激
解决方法
我遇到了类似的问题.我跟随
grokking’s tutorial并且活动从未打开过.我尝试了适用于我的Indrek解决方案.
您应确保在正确的父级下拥有正确的元数据.元数据
<Meta-data android:name="android.app.default_searchable" android:value=".NameSearchActivity" />
应该在申请中.以下元数据
<Meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
应该在AndroidManifest.xml内的搜索活动参数下.