我想在我的android應用程序中使用搜索欄小部件在操作欄中執行搜索。搜索欄小部件不能開始搜索活動
我下面
http://developer.android.com/guide/topics/search/search-dialog.html http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
教程來完成這件事。
我有兩個活動涉及搜索。 SearchBar活動具有操作欄,AcceptSearch是我可搜索的活動。即使我宣佈哪個活動是可搜索的活動,該查詢不會被髮送到AcceptSearch,並且該活動不會啓動。
我配置了搜索欄,使得當widget爲空時出現搜索欄提示,但提示從未出現。這是我的代碼。
搜索欄
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這就是所有需要的信息來重新創建情況。
謝謝大家提前。我很感激。
具有u得到這個問題的解決?如果是,請讓我知道我也在這裏被困住了。 – 2013-07-16 06:04:23
@Lalit你有沒有找到解決方案?我也卡在這裏。 – Solace 2014-12-23 09:18:05