简体   繁体   English

如何将搜索对话框添加到 Android 应用程序

[英]How to add Search Dialog to an Android app

Here is my issue.这是我的问题。 I have a requirement to add a search dialog to an Android app.我需要向 Android 应用程序添加搜索对话框 I have found the way to add a search view component, but not the search dialog.我找到了添加搜索视图组件的方法,但没有找到搜索对话框。 I know from the Android Developers docs that I am supposed to use onSearchRequested() to make it appear, but I don't quite understand what that means.我从 Android 开发人员文档中知道我应该使用onSearchRequested()来让它出现,但我不太明白那是什么意思。 Is the search dialog supposed to be tied to my own UI component that will invoke this method or am I missing something?搜索对话框是否应该绑定到我自己的将调用此方法的 UI 组件,或者我是否遗漏了什么? I haven't managed to read all the docs on searching, but I was hoping someone may point me where I missed something important.我还没有设法阅读所有关于搜索的文档,但我希望有人能指出我错过了什么重要的地方。

I am not well versed in Android development, only a few small projects. Android开发我不是很懂,只会做几个小项目。 I am more of a Flutter dev when creating a mobile app, but I don't have the option this time around.在创建移动应用程序时,我更像是一个 Flutter 开发人员,但这次我没有这个选项。

Below is the screenshot of what the Android Developers docs show as search dialog and the comparison to search view.下面是 Android 开发人员文档显示为搜索对话框的屏幕截图以及与搜索视图的比较。 在此处输入图像描述

Based on the documentation , the steps for invoking a Search Dialog are:根据文档,调用Search Dialog的步骤是:

  1. create an Xml file (which you already did for search widget )创建一个 Xml 文件(你已经为search widget做了)

  2. Declaring a searchable activity: create an actiivty and notify the Manifest that it gets the search results (you did that too)声明一个可搜索的活动:创建一个活动并通知Manifest它获得了搜索结果(你也这样做了)

3. At the Manifest , decalre some other activity (MainActivity for examlpe) to be able to invoke the search dialog. 3.Manifest中,标记一些其他活动(例如 MainActivity)以便能够调用搜索对话框。 Add this line inside <activity> tag:<activity>标签内添加这一行:

<meta-data android:name="android.app.default_searchable"
            android:value=".SearchableActivity" />

( android:value is the name of the Activity that gets the searcg results, which you defined at step 2) android:value是获取搜索结果的 Activity 的名称,您在第 2 步中定义了该名称)

4. At that Activity, create a search button etc and invoke the Serach Dialog like this: 4.在该活动中,创建一个搜索按钮等并调用Serach Dialog ,如下所示:

onSearchRequested();

Very simple actually:)其实很简单:)

The search words stored in intent.getStringExtra(SearchManager.QUERY) as a String, you get it at onCreate() of SearchableActivity class. From this point you should handle the search query and results logic...存储在intent.getStringExtra(SearchManager.QUERY)中的搜索词作为字符串,您可以在SearchableActivity class 的onCreate()中获取它。从这一点开始,您应该处理搜索查询和结果逻辑......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM