[英]How can I use Light SearchView on Dark App Bar on android?
[英]How can I add a searchview to my Android app?
我想在我的 Android 應用程序中添加一個搜索視圖,但我無法理解文檔。 我已經添加了
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:includeInGlobalSearch="true"
android:searchSuggestAuthority="dictionary"
android:searchSuggestIntentAction="android.intent.action.VIEW">
</searchable>
到我的 xml 和<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" /
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
我的清單。 但是provider
應該在哪里標記 go? 運行應用程序時,我收到錯誤膨脹 class 異常。 有誰知道好的教程嗎? 謝謝!
這個答案很晚,但正如我所見,其他答案只是answer-links 。 然后,我將嘗試用簡短的示例代碼提供一些解釋。
文檔中描述了添加SearchView
,按照這些步驟操作非常容易。 正如我們在創建搜索界面主題中所讀到的:
<intent-filter>
不需要具有DEFAULT值的<category>
(您通常在<activity>
元素中看到),因為系統使用其組件名稱將ACTION_SEARCH意圖明確地傳遞給您的可搜索活動。
然后,您的清單變為:
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/my_searchable_layout"/>
“傳統上,您的搜索結果應該顯示在 ListView 中,因此您可能希望您的可搜索活動擴展 ListActivity” - 仍然來自 Docs。 所以你的SearchActivity
可能是:
public class SearchActivity extends ListActivity { }
“當用戶從搜索對話框或搜索小部件執行搜索時,系統會創建一個 Intent 並將用戶查詢存儲在其中。然后系統會啟動您聲明的用於處理搜索的活動(“可搜索活動”)並傳達它的意圖” 。 您需要使用onCreate
方法中的Intent
從搜索對話框或小部件中獲取查詢搜索:
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// Receive the query
String query = intent.getStringExtra(SearchManager.QUERY);
// Search method..
doMySearch(query);
}
doMySearch()
可以是AsyncTask
、新Thread
.. 連接到SQLite DataBase
、 SharedPreference
等等。 “存儲和搜索數據的過程對於您的應用程序來說是獨一無二的” 。 話雖如此,您應該創建一個Adapter
以在列表中提供您的結果。
下面是一個簡短的ListActivity
示例,用於SearchActivity
使用 asynctask 填充列表:
public class SearchActivity extends ListActivity {
// Create an array
String[] values;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activity_layout);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
// At the end of doMySearch(), you can populate
// a String Array as resultStringArray[] and set the Adapter
doMySearch(query);
}
}
class doMySearch extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... params) {
// Connect to a SQLite DataBase, do some stuff..
// Populate the Array, and return a succeed message
// As String succeed = "Loaded";
return succeed;
}
@Override
protected void onPostExecute(String result) {
if(result.equals("Loaded") {
// You can create and populate an Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
SearchActivity.this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
}
}
}
最后,我更喜歡將SearchView
小部件與AppCompat或ActionBarSherlock一起使用,它在主題末尾進行了描述。 自從你問了你的問題(3年前^^)以來,我認為它更適應了。 所以,要做:
// Example with AppCompat
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu
getMenuInflater().inflate(R.menu.options_menu, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true); // Iconify the widget
return true;
}
並執行startActivity()
方法將查詢傳遞給onOptionsItemSelected
方法中的SearchActivity
。 然后,您只需將此搜索項添加到您的菜單中,如下所示(不要忘記自定義前綴),就像您在添加操作視圖中看到的那樣:
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
在示例文檔中,您有包含ListView
的Searchable Dictionary演示,並提供了通過Adapter
連接SQLite DataBase
的完整演示。
瞧。 我希望你找到了解決方案,這篇文章將幫助那些想要同樣的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.