[英]Is it possible to pass search context data using the Search Widget (SearchView) only?
根據官方文檔 ,有兩種方法可以提供搜索界面:使用搜索對話框或SearchView
小部件。 我想注意使用這兩種方式傳遞搜索上下文數據 。
所以, 文檔說:
..您可以提供系統發送給您的可搜索活動的意圖中的其他數據。 您可以傳遞APP_DATA包中的附加數據,該包含在ACTION_SEARCH目的中。
要將此類數據傳遞給可搜索的活動,請覆蓋用戶可以執行搜索的活動的onSearchRequested()方法,使用其他數據創建Bundle,並調用startSearch()以激活搜索對話框。 例如:
@Override public boolean onSearchRequested() { Bundle appData = new Bundle(); appData.putBoolean(SearchableActivity.JARGON, true); startSearch(null, false, appData, false); return true; }
..一旦用戶提交查詢,它就會與您添加的數據一起發送到您的可搜索活動。 您可以從APP_DATA Bundle中提取額外數據以優化搜索。 例如:
Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA); if (appData != null) { boolean jargon = appData.getBoolean(SearchableActivity.JARGON); }
這是指搜索對話框。 那么搜索小部件呢?
是否可以僅使用SearchView
小部件傳遞搜索上下文數據?
希望,有人可以給出明確的解釋和/或建議另一種或類似的方式來實現目標。
謝謝!
我發現了解決方案。 甚至兩個解決方案
他們不需要調用onSearchRequested()
因此根本沒有搜索對話框:)
首先,我提供了一些常見的步驟來創建搜索界面,然后給出源問題的解決方案。
我們通過使用以下代碼創建res/menu/options_menu.xml
文件,將搜索視圖添加到App Bar:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/search_string"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
在res/xml/searchable.xml
文件中創建可搜索配置:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" />
在AndroidManifest.xml
聲明兩個活動:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.searchinterface">
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SearchableActivity"
android:label="@string/app_name"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
</application>
</manifest>
創建一個可搜索的活動,我們處理從MainActivity
傳遞的ACTION_SEARCH
目標和搜索上下文數據。 我們從APP_DATA
Bundle
提取額外數據以優化搜索:
public class SearchableActivity extends AppCompatActivity {
public static final String JARGON = "com.example.searchinterface.jargon";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
// use the query to search the data somehow
Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
if (appData != null) {
boolean jargon = appData.getBoolean(SearchableActivity.JARGON);
// use the context data to refine our search
}
}
}
}
現在,我們需要實現我們的MainActivity
類。 因此,我們擴充菜單並配置SearchView
元素。 我們還需要設置SearchView.OnQueryTextListener
並實現其方法,尤其是onQueryTextSubmit()
。 當用戶按下提交按鈕時它被調用,它包含將搜索上下文數據傳遞給SearchableActivity
的主要邏輯。 最后,我們到達了主要答案部分。 正如我所說,有兩種解決方案:
1.使用Bundle
extra創建一個intent並手動將其發送到SearchableActivity
;
這是MainActivity
包含所有必要的內容:
public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private SearchView mSearchView;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(
new ComponentName(this, SearchableActivity.class)));
mSearchView.setOnQueryTextListener(this);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
Intent searchIntent = new Intent(this, SearchableActivity.class);
searchIntent.putExtra(SearchManager.QUERY, query);
Bundle appData = new Bundle();
appData.putBoolean(SearchableActivity.JARGON, true); // put extra data to Bundle
searchIntent.putExtra(SearchManager.APP_DATA, appData); // pass the search context data
searchIntent.setAction(Intent.ACTION_SEARCH);
startActivity(searchIntent);
return true; // we start the search activity manually
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
感謝https://stackoverflow.com/a/22184137/6411150 。
第二個解決方案也放在onQueryTextSubmit()
(但沒有必要):
2.創建搜索上下文數據Bundle
並將其傳遞給SearchView
的setAppSearchData()
方法。
因此,我們不需要創建並傳遞整個搜索意圖並啟動相應的可搜索活動,系統將負責處理它。
這是另一個代碼片段:
/*
You may need to suppress the “restrictedApi” error that you could possibly
receive from this method "setAppSearchData(appData)”.I had to
I’m targetSdkVersion 26. I’m also using Android Studio 3
with the new gradle plugin, which might be causing this.
If you’re not running Android Studio 3 you can simply put
“//noinspection RestrictedApi"
right above the line: mSearchView.setAppSearchData(appData);
*/
@SuppressWarnings("RestrictedApi")
@Override
public boolean onQueryTextSubmit(String query) {
Bundle appData = new Bundle();
appData.putBoolean(SearchableActivity.JARGON, true); // put extra data to Bundle
mSearchView.setAppSearchData(appData); // pass the search context data
return false; // we do not need to start the search activity manually, the system does it for us
}
感謝https://stackoverflow.com/a/38295904/6411150 。
注意:只有支持庫的SearchView
版本( android.support.v7.widget.SearchView
)包含setAppSearchData()
方法,所以請注意。
如果設計適合您的需求,您可以單獨使用SearchView,並向其添加OnQueryTextListener,並在那里處理它。 不需要任何其他內容,沒有Intent,Meta-tags和XML文件。 我這樣做了幾次,文檔對此有點不清楚。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.