Basic usage of Android search controls, android search controls
In Android, search is a very core function. We can use it to search for any information we can obtain. The information can be the contacts, files, and other information stored on the mobile phone, or resources on the network.
In order to provide users with a good search experience, Android provides a search framework to facilitate developers to integrate search functions in their own apps. Next I will learn about this search framework.
In the Android search framework, we provide two types of search UI controls:
The Search Dialog is a Search control that floats above the Activity and is managed by the system. It does not participate in the lifecycle of the Activity.
Search Wiget is a control similar to EditView. You can place it anywhere you need.
Here we will mainly learn how to use the Search Dialog. You can use the Search Widget at http://developer.android.com/guide/topics/search/index.html.
To use Search Dialog, we need to prepare three items:
Based on the above three points, we start to compile a simple App that includes the search function.
1. Compile searchable configuration
The content of the searchable. xml file is as follows and put it under res/xml of the project.
<?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>
App_label generally specifies the App name. app_label is invisible when simple search is used. Only when you add your search function to quick search box, in the searchable list of quick search box.
Search_hint is the string that appears on the Search Dialog when we do not have any input.
2. Create a Searchable Activity
Suppose the name of the created Activity is SearchableActivity, the configuration in AndroidManifest is as follows:
<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>
Android: name = "android. app. searchable" tells Android that the SearchableActivity interface is sensitive to android. intent. action. SEARCH.
Android: resource = "@ xml/searchable" is used to specify the configuration file in step 1.
3. Call the search interface and process user input
We have defined only one interface for processing search keywords. Next we need to define an interface for calling the search interface:
For the sake of simplicity, I just add a button to the MainActiviy interface of the App, and then call the Search interface of Search Dialog through the button.
MainActiviy. onCreate
Private Button button; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) findViewById (R. id. button); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {onSearchRequested (); // call interface of Search Dialog }});}
AndroidManifest configuration for MainActiviy
<activity android:name=".MainActivity" android:label="@string/app_name" > <meta-data android:name="android.app.default_searchable" android:value=".SearchableActivity" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
Where
<Meta-data android: name = "android. app. default_searchable "android: value = ". searchableActivity "/> specifies the right for MainActivity to call Search Dialog and the SearchableActivity interface.
When you click the button on MainActivity, onSearchRequested () is called to activate the Search Dialog. Then, when I enter some keywords in the Search Dialog and press OK, the system will send an Intent containing the Search keyword for us to start the SearchableActivity interface. Therefore, you only need to obtain the intent in the onCreate method of SearchableActivity and then obtain the search keyword to process it.
SearchableActivity. onCreate
@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); Intent intent = getIntent (); if (Intent. ACTION_SEARCH.equals (intent. getAction () {String query = intent. getStringExtra (SearchManager. QUERY); // get the keyword Toast entered by the user. makeText (this, "the query key is" + query, Toast. LENGTH_LONG ). show (); // pop up the keyword entered by the user, simulated search }}
After completing the preceding three steps, a simple search Demo is ready. As follows:
Search for MainActivity
SearchableActivity processing keywords
Source code download http://pan.baidu.com/s/1o6DTcs6