dan*_*ada 13 android searchview
我在操作栏中成功使用搜索小部件按照本指南执行搜索.搜索很好,但我想知道如何在搜索上传递其他变量.我可以覆盖相同的指南状态onSearchRequested(),但这似乎不适用于搜索小部件.
覆盖问题:
@Override
public boolean onSearchRequested() {
Bundle appData = new Bundle();
appData.putString("KEY", "VALUE");
startSearch(null, false, appData, false);
return true;
}
Run Code Online (Sandbox Code Playgroud)在我的活动类中获取包:
protected void onCreate(Bundle savedInstanceState) {
// ...
Intent intent = getIntent();
Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
String value = appData.getString("KEY");
Log.d("VALUE", value);
// ...
}
Run Code Online (Sandbox Code Playgroud)我的应用程序在创建搜索类时崩溃,因为appData它总是如此null.
onSearchRequested() 被调用,但是捆绑包不适合我的onCreate()方法.
来自传递意图的所有额外内容都是{user_query=my-query, query=my-query}.
dan*_*ada 25
似乎唯一的方法是拦截在您的活动中创建的已启用搜索的新活动.为此,我们重写该startActivity()方法.然后我们可以检查以确保活动确实是搜索活动,然后为意图添加额外内容.工作代码如下.
@Override
public void startActivity(Intent intent) {
// check if search intent
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
intent.putExtra("KEY", "VALUE");
}
super.startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以像搜索活动中的任何其他额外内容一样使用以下内容获取额外费用:
mValue = intent.getStringExtra("KEY");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4480 次 |
| 最近记录: |