After Android, Android has a built-in search control, which works with the search button on the ActionBar. It looks pretty good. This time, I thought it was quite good. The advantage of this search is that after you click it, a search box will pop up automatically. After you enter the content, the matching content will pop up to form a list. After you select the content, the page you want to go to will pop up. You need to add SearchManager searchManager = (SearchManager) getSystemService (Context. SEARCH_SERVICE); SearchView searchView = (SearchView) menu. findItem (R. id. search ). getActionView (); searchView. setSearchableInfo (searchManager. getSearchableInfo (getComponentName (); searchView. setIconifiedByDefault (false); then add case R to onOptionsItemSelected. id. search: onSearchRequested (); retu Rn true; then the search function is executed. The Code handleIntent (getIntent () is viewed from the beginning. This method is executed in onCreate and in @ Override protected void onNewIntent (Intent intent) {handleIntent (intent );} it is also executed, indicating that this method is more important than calculation, in fact, this is a search button, because there is a "Search" button or "go" button in the system input method above android4.0, this handleIntent (getIntent ()) the code in this search is private void handleIntent (Intent intent) {// Intent. ACTION_VIEW android: searchSuggestIntentAction = "android. intent. action. VIEW "is still in this view if (Int Ent. ACTION_VIEW.equals (intent. getAction () {// handles a click on a search suggestion; launches activity to show word Intent wordIntent = new Intent (this, WordActivity. class); wordIntent. setData (intent. getData (); startActivity (wordIntent); finish ();} else if (Intent. ACTION_SEARCH.equals (intent. getAction () {// handles a search query Intent. the Intent ACTION_SEARCH is the String qu triggered when I click the button on the right of the search box of the system. Ery = intent. getStringExtra (SearchManager. QUERY); showResults (query) ;}above is the code in handleIntent. The code in handleIntent has two meanings: When Intent is Intent. ACTION_VIEW. at this time, it is inevitable to jump to the specified class of WordActivity. In fact, this ntent. ACTION_VIEW is the interface that you enter by clicking the list in the suggestion after searching for the suggestion. For example, if you click this to execute the intent, a URI is returned, you can use intent. the getData () method returns the returned URI, Which is returned by the system itself. You can see the configuration file searchable. xml file, the code of this file is as follows: <searchable xmlns: android = "http://schemas.android.com/apk/res/android" android: label = "@ string/search_label" android: hint = "@ string/search_hint" android: searchSettingsDescription = "@ string/settings_description" android: searchSuggestAuthority = "com. example. android. searchabledict. dictionaryProvider "android: searchSuggestIntentAction =" android. intent. action. VIEW "android: SearchSuggestIntentData = "content: // com. example. android. searchabledict. DictionaryProvider/dictionary/sugger_que54r" android: searchSuggestSelection = "? "Android: searchSuggestThreshold =" 1 "android: includeInGlobalSearch =" true "> </searchable> is an XML file with android: searchSuggestAuthority configured in AndroidManifest. the provider configured in xml, such as <provider android: name = ". dictionaryProvider "android: authorities =" com. example. android. searchabledict. android: searchSuggestIntentData in DictionaryProvider "/> is configured to return intentData. For example, content: // com. example. android. searchabl Edict. dictionaryProvider/dictionary/sugger_que54r when you are in if (Intent. ACTION_VIEW.equals (intent. getAction () {// handles a click on a search suggestion; launches activity to show word Intent wordIntent = new Intent (this, WordActivity. class); wordIntent. setData (intent. getData (); startActivity (wordIntent); finish ();} intent. getData () is content: // com. example. android. searchabledict. dictionaryProv Ider/dictionary/sugger_que54r/(the ID of the Item you clicked) is a URI with an ID. The ID here is assigned to him in the code, this is explained later. If you click the search button in the lower-right corner to execute the intent ACTION_SEARCH, The showResults method will be executed. Private void showResults (String query) {Cursor cursor = managedQuery (DictionaryProvider. CONTENT_URI, null, selection, new String [] {query}, null); if (cursor = null) {// There are no results mTextView. setText (getString (R. string. no_results, new Object [] {query});} else {// Display the number of results int count = cursor. getCount (); String countString = getResources (). getQuantityString (R. plura Ls. search_results, count, new Object [] {count, query}); mTextView. setText (countString); // Specify the columns we want to display in the result String [] from = new String [] {DictionaryDatabase. KEY_WORD, DictionaryDatabase. KEY_DEFINITION}; // Specify the corresponding layout elements where we want the columns to go int [] to = new int [] {R. id. word, R. id. definition}; // Create a simple cursor ada Pter for the definitions and apply them to the ListView SimpleCursorAdapter words = new SimpleCursorAdapter (this, R. layout. result, cursor, from, to); mListView. setAdapter (words); // Define the on-click listener for the list items mListView. setOnItemClickListener (new OnItemClickListener () {public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {// Build the Intent used to open WordActivity with a specific word Uri Intent wordIntent = new Intent (getApplicationContext (), WordActivity. class); Uri data = Uri. withAppendedPath (DictionaryProvider. CONTENT_URI, String. valueOf (id); wordIntent. setData (data); startActivity (wordIntent) ;}}}}the preceding is the query string used in the showResult method. ManagedQuery is how you search for your own defined search statements and then search the results. Next we will explain the code in String countString = getResources (). getQuantityString (R. plurals. search_results, count, new Object [] {count, query. As long as this code is plurals, plurals is a complex number in String. the configuration file configured in the xml file is as follows: <plurals name = "search_results"> <item quantity = "one"> % 1 $ d result for \ "% 2 $ s \": </item> <item quantity = "other"> % 1 $ d results for \ "% 2 $ s \": </item> </plurals> the negative number of plurals indicates that if the result is a quantity = "one ", the following message is displayed: % 1 $ d result for \ "% 2 $ s \" % 1 $ d and \ "% 2 $ s \" are similar placeholders, the previous value passed in by new Object [] {count, query} is used to differentiate one or more values. It is better to prompt you to search for one result at a time, when there are multiple search results, it is prompted that you have multiple search results, which may be resu in English. The difference in the number of lt and results leads to their design. You can also set <item quantity = "two"> according to your own definition. Next, go to a list and click the data in the list to execute the method you defined. You need to note that the ID of the item is the one you passed in. Let's see the provider, the buildUriMatcher method in the provider is mainly based on your URI matching, such as matcher. addURI (AUTHORITY, SearchManager. SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST); an int value such as SEARCH_SUGGEST will be returned. This method is mainly used to judge in the query method: switch (sURIMatcher. match (uri) {case SEARCH_SUGGEST: if (selectionArgs = null) {throw new IllegalArgumentException ("selectionArgs must be provided For the Uri: "+ uri);} return getSuggestions (selectionArgs [0]); case SEARCH_WORDS: if (selectionArgs = null) {throw new IllegalArgumentException ("selectionArgs must be provided for the Uri:" + uri);} return search (selectionArgs [0]); this will execute your methods, these methods, the system automatically searches when you enter keywords. You need to assign your automatic values to the fields defined by the system. The query statement is similar to the following: String [] columns = new String [] {BaseColumns. _ ID, BaseColumns. _ ID + "AS" + SearchManager. SUGGEST _ COLUMN_INTENT_DATA_ID, CorpInfoColumns. NAME + "AS" + SearchManager. SUGGEST_COLUMN_TEXT_1, CorpInfoColumns. LEGAL_PERSON + "AS" + SearchManager. SUGGEST_COLUMN_TEXT_2}; SQLiteDatabase db = mOpenHelper. getReadableDatabase (); String selection = CorpInfoColumns. NAME + "like? OR "+ CorpInfoColumns. LEGAL_PERSON +" like? "; String [] selectionArgs = new String [] {" % "+ query +" % "," % "+ query +" % "};, you need to give your own _ id row name to SearchManager. SUGGEST_COLUMN_INTENT_DATA_ID. In this way, a URI with ID will be returned before, and the first line you search will be named SearchManager. SUGGEST_COLUMN_TEXT_1, the row name of the second row must be SearchManager. SUGGEST_COLUMN_TEXT_2, which is automatically displayed by the system. Otherwise, a white area is displayed. This adapter can be defined by myself, but I have not defined it. If you are interested, try it.