ผสานรวมฟีเจอร์การค้นหาของ Android เข้ากับแอป
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ลองใช้ Compose
Jetpack Compose เป็นชุดเครื่องมือ UI ที่แนะนำสำหรับ Android ดูวิธีเพิ่มฟังก์ชันการค้นหาใน Compose
Search เป็นฟีเจอร์หลักสำหรับผู้ใช้ใน Android ผู้ใช้ต้องค้นหาข้อมูลที่ตนเองเข้าถึงได้ ไม่ว่าเนื้อหาจะอยู่ในอุปกรณ์หรืออินเทอร์เน็ต Android มี
เฟรมเวิร์กการค้นหาที่จะช่วยคุณติดตั้งใช้งานการค้นหาสำหรับแอปพลิเคชันของคุณ เพื่อช่วยสร้างประสบการณ์การค้นหาที่สอดคล้องกันสำหรับผู้ใช้
รูปที่ 1 กล่องโต้ตอบการค้นหาที่มี
คำแนะนำในการค้นหาที่กำหนดเอง
เฟรมเวิร์กการค้นหามีโหมดอินพุตการค้นหา 2 โหมด ได้แก่ กล่องโต้ตอบการค้นหาที่ด้านบนของหน้าจอ
หรือวิดเจ็ตการค้นหา (SearchView
) ที่คุณฝังในเลย์เอาต์กิจกรรมได้
ไม่ว่าจะในกรณีใด ระบบ Android จะช่วยในการติดตั้งใช้งานการค้นหาโดย
ส่งคำค้นหาไปยังกิจกรรมที่เฉพาะเจาะจงซึ่งทำการค้นหา นอกจากนี้ คุณยังเปิดใช้
กล่องโต้ตอบหรือวิดเจ็ตการค้นหาเพื่อให้คำแนะนำในการค้นหาขณะที่ผู้ใช้พิมพ์ได้ด้วย รูปที่ 1 แสดงตัวอย่างกล่องโต้ตอบการค้นหาพร้อมคำแนะนำการค้นหาที่ไม่บังคับ
เมื่อตั้งค่ากล่องโต้ตอบการค้นหาหรือวิดเจ็ตการค้นหาแล้ว คุณจะทำสิ่งต่อไปนี้ได้
- เปิดใช้การค้นหาด้วยเสียง
- แสดงคำแนะนำการค้นหาตามคำค้นหาล่าสุดของผู้ใช้
- แสดงคำแนะนำในการค้นหาที่กำหนดเองซึ่งตรงกับผลลัพธ์จริงในข้อมูลแอปพลิเคชัน
- แสดงคำแนะนำการค้นหาของแอปพลิเคชันในช่องค้นหาด่วนทั่วทั้งระบบ
หมายเหตุ: เฟรมเวิร์กการค้นหาไม่มี API สำหรับ
ค้นหาข้อมูล หากต้องการค้นหา คุณต้องใช้ API ที่เหมาะสมกับข้อมูลของคุณ เช่น หากข้อมูลจัดเก็บอยู่ในฐานข้อมูล SQLite ให้ใช้ android.database.sqlite
API เพื่อทำการค้นหา
นอกจากนี้ เราไม่รับประกันว่าอุปกรณ์จะมีปุ่ม SEARCH โดยเฉพาะที่จะเรียกใช้
อินเทอร์เฟซการค้นหาในแอปพลิเคชันของคุณ เมื่อใช้กล่องโต้ตอบการค้นหาหรืออินเทอร์เฟซที่กำหนดเอง คุณต้องระบุปุ่มค้นหาใน UI ที่เปิดใช้งานอินเทอร์เฟซการค้นหา ดูข้อมูลเพิ่มเติมได้ที่เรียกใช้กล่องโต้ตอบการค้นหา
หน้าต่อไปนี้จะแสดงวิธีใช้เฟรมเวิร์กของ Android เพื่อติดตั้งใช้งานการค้นหา
- สร้างอินเทอร์เฟซการค้นหา
- วิธีตั้งค่าแอปพลิเคชันให้ใช้กล่องโต้ตอบการค้นหาหรือวิดเจ็ตการค้นหา
- เพิ่มคำแนะนำคำค้นหาล่าสุด
- วิธีให้คำแนะนำตามคำค้นหาที่ใช้ก่อนหน้านี้
- เพิ่มคำแนะนำที่กำหนดเอง
- วิธีแสดงคำแนะนำโดยอิงตามข้อมูลที่กำหนดเองจากแอปพลิเคชันของคุณและนำเสนอในช่องค้นหาด่วนทั่วทั้งระบบ
- การกำหนดค่าที่ค้นหาได้
- เอกสารอ้างอิงสำหรับไฟล์การกำหนดค่าที่ค้นหาได้ เอกสารอื่นๆ
ยังกล่าวถึงไฟล์การกำหนดค่าในแง่ของลักษณะการทำงานที่เฉพาะเจาะจงด้วย
ปกป้องความเป็นส่วนตัวของผู้ใช้
เมื่อติดตั้งใช้งานการค้นหาในแอปพลิเคชัน ให้ทำตามขั้นตอนเพื่อปกป้องความเป็นส่วนตัวของผู้ใช้
ผู้ใช้หลายคนถือว่ากิจกรรมในโทรศัพท์ของตน รวมถึงการค้นหา เป็นข้อมูลส่วนตัว
โปรดปฏิบัติตามหลักการต่อไปนี้เพื่อปกป้องความเป็นส่วนตัวของผู้ใช้
- อย่าส่งข้อมูลส่วนบุคคลไปยังเซิร์ฟเวอร์ และหากจำเป็นต้องส่ง ก็อย่าบันทึกข้อมูลดังกล่าว
ข้อมูลส่วนบุคคลคือข้อมูลใดๆ ที่สามารถระบุตัวผู้ใช้ได้ เช่น ชื่อ อีเมล ข้อมูลการเรียกเก็บเงิน หรือข้อมูลอื่นๆ ที่ลิงก์กับข้อมูลดังกล่าวได้อย่างสมเหตุสมผล หากแอปพลิเคชันของคุณใช้การค้นหาโดยอาศัยเซิร์ฟเวอร์ โปรดหลีกเลี่ยงการส่งข้อมูลส่วนบุคคลพร้อมกับคำค้นหา เช่น หากคุณค้นหาธุรกิจ
ใกล้รหัสไปรษณีย์
ก็ไม่จำเป็นต้องส่งรหัสผู้ใช้ด้วย ให้ส่งเฉพาะรหัสไปรษณีย์ไปยังเซิร์ฟเวอร์ หากจำเป็นต้อง
ส่งข้อมูลส่วนบุคคล ให้หลีกเลี่ยงการบันทึกข้อมูล หากจำเป็นต้องบันทึก ให้ปกป้องข้อมูลนั้น
อย่างระมัดระวังและลบข้อมูลโดยเร็วที่สุด
- มอบวิธีให้ผู้ใช้ล้างประวัติการค้นหา
เฟรมเวิร์กการค้นหาช่วยให้แอปพลิเคชันของคุณแสดงคำแนะนำที่เฉพาะเจาะจงตามบริบทขณะที่ผู้ใช้
พิมพ์ บางครั้งคำแนะนำเหล่านี้จะอิงตามการค้นหาก่อนหน้าหรือการดำเนินการอื่นๆ ที่ผู้ใช้ทำในเซสชันก่อนหน้า
ผู้ใช้อาจไม่ต้องการให้ผู้ใช้คนอื่นๆ ในอุปกรณ์เห็นการค้นหาก่อนหน้านี้ หากแอปพลิเคชันของคุณแสดงคำแนะนำที่
สามารถเปิดเผยกิจกรรมการค้นหาก่อนหน้าได้ ให้ใช้วิธีที่ผู้ใช้สามารถล้าง
ประวัติการค้นหาของตนเอง หากคุณใช้ SearchRecentSuggestions
คุณจะเรียกใช้เมธอด
clearHistory()
ได้ หากคุณใช้คำแนะนำที่กำหนดเอง คุณต้องระบุวิธีการ "ล้างประวัติ"
ที่คล้ายกันในผู้ให้บริการเนื้อหาซึ่งผู้ใช้สามารถดำเนินการได้
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-26 UTC
[null,null,["อัปเดตล่าสุด 2025-08-26 UTC"],[],[],null,["Try the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add search functionality in Compose. \n[Search bar →](/develop/ui/compose/components/search-bar) \n\nSearch is a core user feature on Android. Users must be able\nto search any data that is available to them, whether the content is located on the device or\nthe internet. To help create a consistent search experience for users, Android provides a\nsearch framework that helps you implement search for your application. \n\n**Figure 1.** A search dialog with custom\nsearch suggestions.\n\nThe search framework offers two modes of search input: a search dialog at the top of the\nscreen or a search widget ([SearchView](/reference/android/widget/SearchView)) that you can embed in your activity\nlayout. In either case, the Android system assists your search implementation by\ndelivering search queries to a specific activity that performs searches. You can also enable\nthe search dialog or widget to provide search suggestions as the user types. Figure 1 shows an\nexample of the search dialog with optional search suggestions.\n\nOnce you set up either the search dialog or the search widget, you can do the following:\n\n- Enable voice search.\n- Provide search suggestions based on recent user queries.\n- Provide custom search suggestions that match actual results in your application data.\n- Offer your application's search suggestions in the system-wide Quick Search Box.\n\n**Note** : The search framework does *not* provide APIs to\nsearch your data. To perform a search, you need to use APIs appropriate for your data. For example,\nif your data is stored in an SQLite database, use the [android.database.sqlite](/reference/android/database/sqlite/package-summary)\nAPIs to perform searches.\n\n\u003cbr /\u003e\n\n\nAlso, there is no guarantee that a device provides a dedicated SEARCH button that invokes the\nsearch interface in your application. When using the search dialog or a custom interface, you\nmust provide a search button in your UI that activates the search interface. For more\ninformation, see [Invoke the search\ndialog](/develop/ui/views/search/search-dialog#InvokingTheSearchDialog).\n\nThe following pages show you how to use Android's framework to implement search:\n\n**[Create a search interface](/develop/ui/views/search/search-dialog)**\n: How to set up your application to use the search dialog or search widget.\n\n**[Add recent query\nsuggestions](/develop/ui/views/search/adding-recent-query-suggestions)**\n: How to provide suggestions based on queries previously used.\n\n**[Add custom suggestions](/develop/ui/views/search/adding-custom-suggestions)**\n: How to provide suggestions based on custom data from your application and offer them\n in the system-wide Quick Search Box.\n\n**[Searchable configuration](/develop/ui/views/search/searchable-config)**\n: A reference document for the searchable configuration file. The other\n documents also discuss the configuration file in terms of specific behaviors.\n\nProtect user privacy\n\nWhen you implement search in your application, take steps to protect the user's\nprivacy. Many users consider their activities on their phone---including searches---to\nbe private information. To protect users' privacy, abide by the following\nprinciples:\n\n- **Don't send personal information to servers‐and if you must, don't log it.**\n\n Personal information is any information that can personally identify your users, such as their\n names, email addresses, billing information, or other data that can be reasonably linked to such\n information. If your application implements search with the assistance of a server, avoid sending\n personal information along with the search queries. For example, if you are searching for businesses\n near a ZIP code,\n you don't need to send the user ID as well; send only the ZIP code to the server. If you must\n send personal information, avoid logging it. If you must log it, protect that data\n very carefully and erase it as soon as possible.\n- **Provide users with a way to clear their search history.**\n\n The search framework helps your application provide context-specific suggestions while the user\n types. Sometimes these\n suggestions are based on previous searches or other actions taken by the user in an earlier\n session. A user might not want previous searches to be revealed to other device users. If your\n application provides suggestions that\n can reveal previous search activities, implement a way for the user to clear their\n search history. If you are using [SearchRecentSuggestions](/reference/android/provider/SearchRecentSuggestions),\n you can call the\n [clearHistory()](/reference/android/provider/SearchRecentSuggestions#clearHistory())\n method. If you are implementing custom suggestions, you need to provide a similar \"clear history\"\n method in your content provider that the user can execute."]]