我想实现我在操作栏使用搜索栏小工具Android应用搜索。
I'm trying to implement a search on my android application using a search bar widget in the action bar.
我下面
http://developer.android.com/guide/topics/search/search-dialog.html http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
要完成这件事的教程。
我参与搜索的两项活动。搜索栏活动有操作栏和AcceptSearch是我的搜索活动。即使我宣布该活动是搜索的活动,查询不被发送到AcceptSearch和活动没有启动。
I have two activities involved in searching. SearchBar activity has the action bar and AcceptSearch is my searchable activity. Even though I declared which activity was the searchable activity, the query is not being sent over to AcceptSearch and the activity is not launched.
我配置,使得当构件是空的,但暗示永远不会出现任何一个搜索栏提示显示的搜索栏。这是我的code。
I configured the search bar such that a search bar hint appears when the widget is empty, but the hint never appears either. Here is my code.
搜索栏
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>
我beleive这就是所有必要的信息,以重现这种情况。
I beleive thats all the needed information to recreate the situation.
感谢大家提前。我AP precicate。
Thank you all in advance. I apprecicate.
我也有类似的问题。我在下面的 grokking的教程并从未打开过的活动。我试过Indrek的解决方案,我对我的作品。
I had a similar problem. I was following grokking's tutorial and the activity never opened. I tried the Indrek's solutions I that works for me.
您应该确保您有在合适的父权的元数据。元数据
You should be sure that you have the right meta-data under the right parent. The meta-data
<meta-data
android:name="android.app.default_searchable"
android:value=".NameSearchActivity" />
应该是在应用程序。后续的元数据。
should be under application. The follow meta-data
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
应该是里面AndroidManifest.xml中搜索活动的参数下。
should be under the search activity parameter inside AndroidManifest.xml.
在搜索活动我没有什么好说的,用于管理活动的叠前挂的教程。我希望这个解决方案适用于每个人同样的问题。
In the search Activity I did what it say in the tutorial linked before for managing the activity stack. I hope that this solution works for everyone with the same problem.