【发布时间】:2011-04-16 23:04:05
【问题描述】:
我没有使用 SearchManager,但我已经构建了自己的自定义搜索活动。我希望在用户单击“搜索”按钮时显示它。我该怎么做?
【问题讨论】:
标签: android search android-activity android-searchmanager
我没有使用 SearchManager,但我已经构建了自己的自定义搜索活动。我希望在用户单击“搜索”按钮时显示它。我该怎么做?
【问题讨论】:
标签: android search android-activity android-searchmanager
在您自己的应用程序中,您可以通过监控 onKeyDown()、AFAIK 来做到这一点。
在其他应用程序中,除非通过自定义固件,否则这是不可能的。
【讨论】:
Activity.onSearchRequested() 似乎是一个更好的选择。
onSearchRequested()。如果没有,只需覆盖onKeyDown() 并注意KEYCODE_SEARCH 事件。
如果我理解正确,您是在询问如何启动您的搜索活动。为您的按钮创建一个 onClickListener 和一个 EditText 字段供用户输入文本。在您的搜索活动中查询意图,以便在用户单击“搜索”按钮时获取用户正在搜索的任何内容。
EditText et = new EditText(this);
public void onClick(View v){
String searchText = et.getText();
Intent intent = new Intent(this, com.example.app.SearchActivity.class);
intent.putExtra("searchQuery", searchText);
startActivity(intent);
}
http://developer.android.com/guide/appendix/faq/commontasks.html#opennewscreen
【讨论】:
不确定这是否可行,但您是否尝试过扩展可以捕捉搜索意图的 BroadcastReceiver?检查开发人员参考的意图似乎是“android.search.action.GLOBAL_SEARCH”。所以,你会有一个像这样的 Receiver 类:
public class MyIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.search.action.GLOBAL_SEARCH")) {
Intent sendIntent = new Intent(context, MySearchActivity.class)
context.startActivity(intent);
}
}
}
在你的清单中,在应用程序标签之间,你应该有
<receiver android:name="MyIntentReceiver">
<intent-filter>
<action android:name="android.search.action.GLOBAL_SEARCH" />
</intent-filter>
</receiver>
【讨论】: