gpt4 book ai didi

android - 是否可以仅使用搜索小部件 (SearchView) 传递搜索上下文数据?

转载 作者:太空狗 更新时间:2023-10-29 15:51:02 33 4
gpt4 key购买 nike

根据official documentation ,有两种提供搜索界面的方法:使用搜索对话框或 SearchView 小部件。我想注意使用这两种方式传递搜索上下文数据

因此,documentation说:

..you can provide additional data in the intent that the system sends to your searchable activity. You can pass the additional data in the APP_DATA Bundle, which is included in the ACTION_SEARCH intent.

To pass this kind of data to your searchable activity, override the onSearchRequested() method for the activity from which the user can perform a search, create a Bundle with the additional data, and call startSearch() to activate the search dialog. For example:

@Override
public boolean onSearchRequested() {
Bundle appData = new Bundle();
appData.putBoolean(SearchableActivity.JARGON, true);
startSearch(null, false, appData, false);
return true;
}

..Once the user submits a query, it's delivered to your searchable activity along with the data you've added. You can extract the extra data from the APP_DATA Bundle to refine the search. For example:

Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA);
if (appData != null) {
boolean jargon = appData.getBoolean(SearchableActivity.JARGON);
}

这是指搜索对话框。那么搜索小部件呢?

是否可以仅使用 SearchView 小部件传递搜索上下文数据?

希望有人能给出明确的解释和/或建议另一种或类似的方法来实现目标。

谢谢!

最佳答案

我找到了解决方案。甚至两个解决方案!

他们不需要调用 onSearchRequested() 因此根本没有搜索对话框 :)

首先,我提供了一些创建搜索界面的常用步骤,然后给出了源问题的解决方案。

我们通过使用以下代码创建 res/menu/options_menu.xml 文件,将搜索 View 添加到应用栏:

<?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中声明两个 Activity :

<?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>

创建一个可搜索 Activity ,我们在其中处理从 MainActivity 传递的 ACTION_SEARCH Intent 和搜索上下文数据。我们从 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 并将其传递给 SearchViewsetAppSearchData() 方法。

因此,我们不需要创建和传递整个搜索 Intent 并启动相应的可搜索 Activity ,系统会处理它。

这是另一个代码 fragment :

/*
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( ) 方法,所以要注意。

关于android - 是否可以仅使用搜索小部件 (SearchView) 传递搜索上下文数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39070069/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com