On Android, search is a core user feature. Users can search for any data that is available, regardless of whether the content is saved to the device itself or needs to be accessed over the network. Android provides a search framework for users to create a consistent search experience that can help you implement a search application. The search framework provides two search modes: A search dialog box (Dialog) is located at the top of the search screen or in the Widgets (Searchview), which you can embed into your layout. In both cases, the Android system will help you search for a specific activity to implement the task of performing a search. The effect is as shown.
When a user needs to perform a search for a search dialog or search widget, the system creates a intent and store user query, and then the system launches the activity that you declared in advance to perform the data lookup, using it for data search. A total of three steps are required.
(1) Create a search.xml file in the Res/xml file
An XML file that configures settings for some search dialog or widgets. It includes setting features such as voice search, search suggestions, and hint text search boxes.
<?xml version= "1.0" encoding= "Utf-8"? ><searchable xmlns:android= "http://schemas.android.com/apk/res/ Android " android:label=" @string/app_label " android:hint=" @string/search_hint "></searchable>
(2) Declaring a searchactivity
This activiy receives data from the search user and displays the search results. It is especially important to note that in order for the call activity to find this searchactivity, you must first declare the Android search Activitydao to your manifest file. as shown below.
<application > <activity android:name= ". Searchableactivity "> <intent-filter> <action android:name=" Android.intent.action.SEARCH "/ > </intent-filter> <meta-data android:name= "android.app.searchable" android:resource= "@ Xml/searchable "/> </activity> ...</application>
Searchactivity to complete the following three steps. 1 Receive Query 2 Search your Data 3 show the results.
@Overridepublic void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.search); Handleintent (Getintent ());} @Overrideprotected void Onnewintent (Intent Intent) { setintent (Intent); Handleintent (intent);} private void Handleintent (Intent Intent) { if (Intent.ACTION_SEARCH.equals (Intent.getaction ())) { String query = Intent.getstringextra (searchmanager.query); Domysearch (query);} }
Before looking at the blog and saw someone say Onnewintent () do not know when to promote the hair, I studied the official document above is written. If we set Android:launchmode to "Singletop", then serachactivity receive Action_search intent calls the Onnewintent () method.
(3) Create a search interface
The search interface contains two types of Searchdialog and Searchwidget. Serachdialog: By default, the search dialog is hidden, and we make it appear at the top of the screen by calling Onsearchrequested () (when the user presses the search button), Searchwidget: Allows the user to place the search box anywhere in the layout, But it usually needs to be combined with Actionbar.
The activity of the search interface also needs to be declared in the manifest file
<activity android:name= ". Otheractivity "... > <!--Enable the search dialog to send searches to searchableactivity-- <meta-da Ta android:name= "android.app.default_searchable" android:value= ". Searchableactivity "/> </activity>
public class Otheractivity extends activity{private Button mstartsearch; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.search_invoke); Just a button Mstartsearch = (button) Findviewbyid (R.id.btn_start_search); Start the search box Mstartsearch.setonclicklistener (new Onclicklistener () {@Override P ublic void OnClick (View v) {onsearchrequested (); } }); }//Overriding the Onsearchrequested method @Override public boolean onsearchrequested () { In addition to the value of the input query, you can also bind some data bundle Appsearchdata = new bundle (); Appsearchdata.putstring ("Demo_key", "text"); Startsearch (null, FALSE, Appsearchdata, false); Must return True. Otherwise the bound data is voided return true; } }
Development and use of core features of Android Searchview (source code sharing)