【问题标题】:How to define an activity for the Search Button?如何为搜索按钮定义一个活动?
【发布时间】:2011-04-16 23:04:05
【问题描述】:

我没有使用 SearchManager,但我已经构建了自己的自定义搜索活动。我希望在用户单击“搜索”按钮时显示它。我该怎么做?

【问题讨论】:

标签: android search android-activity android-searchmanager


【解决方案1】:

在您自己的应用程序中,您可以通过监控 onKeyDown()、AFAIK 来做到这一点。

在其他应用程序中,除非通过自定义固件,否则这是不可能的。

【讨论】:

  • Activity.onSearchRequested() 似乎是一个更好的选择。
  • 你能用代码告诉我,我如何监控搜索按钮的 onKeyDown() 吗?
  • @Sheehan Alam:我先试试 Christopher 的 onSearchRequested()。如果没有,只需覆盖onKeyDown() 并注意KEYCODE_SEARCH 事件。
【解决方案2】:

如果我理解正确,您是在询问如何启动您的搜索活动。为您的按钮创建一个 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

【讨论】:

  • 我知道如何开始我的活动,但我希望它与所有手机上的搜索按钮相关联。所以当你点击放大镜按钮时,它会调用我的活动。
【解决方案3】:

不确定这是否可行,但您是否尝试过扩展可以捕捉搜索意图的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 2012-03-28
    • 2011-10-06
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多