Android Search suggestions (search Lenovo)

Source: Internet
Author: User
Tags lenovo

Android search suggestions, in short, that is, when the user input a keyword, the system automatically gives several search suggestions containing similar keywords.

As follows:


Mainactivity.java

Package Zhangphil.search;import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.provider.searchrecentsuggestions;import Android.view.view;import Android.widget.button;public Class Mainactivity extends Actionbaractivity {@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); Button search = (button) Findviewbyid (R.id.search); Search.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {//initiates search ();}); Button clear = (Button) Findviewbyid (r.id.clear); Clear.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {clear ();}}); private void Search () {//Initiate a search request to the Android system. This.onsearchrequested ();} The onsearchrequested () method can be overloaded or not overloaded. Depends on whether more parameters or data are passed to the next operation. It is common for code to run logic to pass more data to the next action, and this method can be overloaded to complete more data delivery. If you do not overload, you can. The Android system launches the default search box. (Above the UI) @Overridepublic Boolean onsearchrequested () {Bundle bundle = new Bundle (); Bundle.puTstring ("Some key", "some Value") this.startsearch ("Input search keyword"/** initialize the prompt word in the search box */, True, bundle, False/** here if true, The search that will start the global settings on the phone, such as Google search */); return true; Clears all historical search records private void Clear () {searchrecentsuggestions suggestions = new Searchrecentsuggestions (This, Zhangphilsuggestionprovider.authority,zhangphilsuggestionprovider.mode);//for user privacy, provide the app with the option to clear all user search history. Suggestions.clearhistory ();}}

Onsearchrequested will trigger the Android system to automatically pop up the search box. Mainactivity Required Layout file Activity_main.xml is simple, two buttons, one triggering search event, one triggering clear search history action:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:orientation=" vertical "    android:layout_width=" fill_parent "    android:layout_height=" Fill_parent ">    <button        android:id=" @+id/search "        android:layout_width=" Wrap_content        " android:layout_height= "Wrap_content"        android:text= "@string/search"/>    <button        android:id= "@+ Id/clear "        android:layout_width=" wrap_content "        android:layout_height=" wrap_content "        android:text = "@string/clear"/>    </LinearLayout>


Content of Androidmanifest.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Zhangphil.search "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk         android:minsdkversion= "8" android:targetsdkversion= "/>" <application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme "> <activity android:name=". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER "/> </intent-filter> <meta-data android:name=" Android.app.default_ Searchable "android:value=".        Searchactivity "/> </activity>        <activity android:name= ". Searchactivity "android:label=" @string/app_name "> <intent-filter> <actio                n android:name= "Android.intent.action.SEARCH"/> </intent-filter> <meta-data         Android:name= "android.app.searchable" android:resource= "@xml/searchable"/> </activity> <provider android:name= ". Zhangphilsuggestionprovider "android:authorities=" zhangphil_authority "/> </application></manife St>


Searchactivity.java file:

Package Zhangphil.search;import Android.app.activity;import Android.app.searchmanager;import Android.content.intent;import Android.os.bundle;import Android.provider.searchrecentsuggestions;import Android.widget.toast;public class Searchactivity extends Activity {@Overridepublic void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Intent Intent = Getintent (); if (Intent.ACTION_SEARCH.equals (Intent.getaction ())) {//Save keyword Savemyrecentquery (intent);}} private void Savemyrecentquery (Intent Intent) {//The search keyword is saved to the Android system's own database. If customization is not necessary, this process can use a standard, common method. String query = Intent.getstringextra (searchmanager.query); Searchrecentsuggestions suggestions = new Searchrecentsuggestions (this,zhangphilsuggestionprovider.authority, Zhangphilsuggestionprovider.mode); suggestions.saverecentquery (query, NULL); Toast.maketext (This, "search keywords:" + query, Toast.length_long). Show ();}}




Zhangphilsuggestionprovider.java

Package Zhangphil.search;import Android.content.searchrecentsuggestionsprovider;public Class Zhangphilsuggestionprovider Extendssearchrecentsuggestionsprovider {//can arbitrarily define a distinguishable string, but note the globally unique same reference. Public final static String authority = "Zhangphil_authority";p ublic final static int MODE = Database_mode_queries;public Z Hangphilsuggestionprovider () {setupsuggestions (authority, MODE);}}


Searchable.xml

<?xml version= "1.0" encoding= "Utf-8"? ><searchable xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:label=" @string/app_name "    android:hint=" @string/search_hint "     android: Searchsuggestauthority= "zhangphil_authority"    android:searchsuggestselection= "?" ></searchable>

Complete project All code files I have uploaded to Csdn, you can click download link to download to local, unzip and import into Eclipse to run the demo. CSDN Download Link:

http://download.csdn.net/detail/zhangphil/8603509



Android Search suggestions (search Lenovo)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.