Android built-in Search dialog box (floating search) Example

Source: Internet
Author: User

First:

Steps:

(1) configure the search bar and create a searchable. xml configuration file under res/xml.

<?xml version="1.0" encoding="utf-8"?><searchable  xmlns:android="http://schemas.android.com/apk/res/android"  android:hint="@string/searchHint"   android:searchMode="showSearchLabelAsBadge"    android:searchSuggestAuthority="com.android.cbin.SearchSuggestionSampleProvider"    android:searchSuggestSelection=" ? ">  </searchable>

(2) In manifest. xml configuration, there are two scenarios for the Activity to be processed by the search result. One is to open an Activtiy from the search bar in other activities.

Specifically, the search results are processed. The second is that the current Activity is the Activity that processes the results. This configuration contains two situations that can be distinguished by the Code.

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.android.cbin"      android:versionCode="1"      android:versionName="1.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".Main"                  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=".SearchResultActivity" />        </activity>    <activity android:name="SearchResultActivity" android:launchMode="singleTop">            <intent-filter>            <action android:name="android.intent.action.SEARCH"></action>        </intent-filter>        <meta-data android:resource="@xml/searchable" android:name="android.app.searchable"></meta-data>    </activity><provider android:name="SearchSuggestionSampleProvider" android:authorities="com.android.cbin.SearchSuggestionSampleProvider"></provider></application>    <uses-sdk android:minSdkVersion="7" /></manifest> 
(3) The authorities point to the SearchSuggestionSampleProvider associated with the name. It is a subclass of SearchRecentSuggestionsProvider.
package com.android.search;import android.content.SearchRecentSuggestionsProvider;public class SearchSuggestionSampleProvider extends        SearchRecentSuggestionsProvider {    final static String AUTHORITY="com.android.search.SearchSuggestionSampleProvider";    final static int MODE=DATABASE_MODE_QUERIES;        public SearchSuggestionSampleProvider(){        super();        setupSuggestions(AUTHORITY, MODE);    }}

(4) to use the search bar, we must override the onSearchRequested method of the Activity to start a search bar on the interface.

However, this action is not automatically triggered and must be triggered by a button or menu click event;

Package com. android. search; import com. android. search. r; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; public class Main extends Activity implements OnClickListener {/** Called when the activity is first created. */private EditText etdata; private Button btnsearch; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); findview ();} private void findview () {etdata = (EditText) findViewById (R. id. etdata); btnsearch = (Button) findViewById (R. id. btncall); btnsearch. setOnClickListener (this) ;}@ Override public void onClick (View v) {// TODO Auto-generated method stub onSearchRequested () ;}@ Override public boolean onSearchRequested () {String text = etdata. getText (). toString (); Bundle bundle = new Bundle (); bundle. putString ("data", text); // open the floating search box (the value added to the search box by default for the first parameter) // bundle is the transmitted data startSearch ("Haha", false, bundle, false); // This must be returned if it is only super. onSearchRequested method // not only does onSearchRequested (default value in the search box) fail to be added to the search box // bundle nor return true ;}}

(5) search in this Activity

Package com. android. search; import com. android. search. r; import android. app. activity; import android. app. searchManager; import android. content. intent; import android. OS. bundle; import android. provider. searchRecentSuggestions; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class SearchResultActivity extends Activity imple Ments OnClickListener {private TextView tvquery, tvdata; private Button btnsearch; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. searchresult); tvquery = (TextView) findViewById (R. id. tvquery); tvdata = (TextView) findViewById (R. id. tvdata); btnsearch = (Button) findViewById (R. id. btnSearch); doSearchQuery (); btnsearch. setOnClickListen Er (this);} public void doSearchQuery () {final Intent intent = getIntent (); // obtain the String query = intent in the search box. getStringExtra (SearchManager. QUERY); tvquery. setText (query); // Save the search record SearchRecentSuggestions suggestions = new SearchRecentSuggestions (this, SearchSuggestionSampleProvider. AUTHORITY, SearchSuggestionSampleProvider. MODE); suggestions. saveRecentQuery (query, null); if (Intent. ACTION_SEARCH.equals (I Ntent. getAction () {// get the passed data Bundle bundled = intent. getBundleExtra (SearchManager. APP_DATA); if (bundled! = Null) {String ttdata = bundled. getString ("data"); tvdata. setText (ttdata);} else {tvdata. setText ("no data") ;}}@ Override public void onClick (View v) {// TODO Auto-generated method stub onSearchRequested ();} @ Override public boolean onSearchRequested () {startSearch ("onNewIntent", false, null, false); return true ;}@ Override public void onNewIntent (Intent intent) {super. onNewIntent (intent); // get String query = intent in the search box. getStringExtra (SearchManager. QUERY); tvquery. setText (query); // Save the search record SearchRecentSuggestions suggestions = new SearchRecentSuggestions (this, SearchSuggestionSampleProvider. AUTHORITY, SearchSuggestionSampleProvider. MODE); suggestions. saveRecentQuery (query, null); if (Intent. ACTION_SEARCH.equals (intent. getAction () {// get the passed data Bundle bundled = intent. getBundleExtra (SearchManage R. APP_DATA); if (bundled! = Null) {String ttdata = bundled. getString ("data"); tvdata. setText (ttdata);} else {tvdata. setText ("no data ");}}}}

 

 

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.