There are two ways to search for Android:
Search Dialog
It is a UI Component that, when activated, displays a floating search box at the top.
Searchview
Can be laid out anywhere. Available in Android 3.0 (level 11). Searchview simple, casual use, here is mainly about the basic use of Search Dialog, because Xamarin is slightly different from native Android. Effect: Source: Https://github.com/gruan01/Xamarin-Example/tree/master/Search searchable to use search Dialog you need to configure a search configuration file: Put it in the Resources/xml directory. If the XML directory does not exist, you need to create one manually. File name casually, generally take searchable.xml
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <searchablexmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:label= "@string/searchlabel"4 Android:hint= "@string/searchplaceholder"5 >6 </searchable>
The root node must be searchable.
Android:label is a required entry
Android:hint is the placeholder in HTML5.
For other genus Options, please refer to:
Http://developer.android.com/guide/topics/search/searchable-config.html
? Activity enable search Diaog feature to turn on searching Dialog, you must also turn on activity on the active Android App to directly modify the manifest file. As follows:
1 <ActivityAndroid:name= "Search" >2 <Intent-filter>3 <ActionAndroid:name= "android.intent.action.SEARCH" />4 </Intent-filter>5 <Meta-dataAndroid:name= "android.app.searchable"Android:resource= "@xml/searchable"/>6 </Activity>7 8 <ActivityAndroid:name=". Mainactivity " >9 <Meta-dataAndroid:name= "android.app.default_searchable"Ten Android:value=". Search " /> One </Activity>
Android:resource= "@xml/searchable" means that the above configuration file (Xml/searchable.xml) is marked in yellow as a fixed string. This section can be consulted: http://developer.android.com/guide/topics/search/search-dialog.html under Xamarin, the manifest file is the properties below Androidmanifest.xml
This file is maintained by Xamarin itself and is not recommended for direct modification. To start the Activity search, you can do this:? 1, create a new searchactivity
1[MetaData ("android.app.searchable", Resource ="@xml/searchable")]2[Intentfilter (New[] {intent.actionsearch})]3[Activity (Label ="searchactivity",Name = "aa.searcha" )]//Aa.searcha, AA is the package name and must start with a lowercase letter4 Public classsearchactivity:activity {5 protected Override voidOnCreate (Bundle bundle) {6 Base. OnCreate (bundle);7 8 9 if( This. Intent.Action.Equals (Intent.actionsearch)) {Ten varquery = This. Intent.getstringextra (searchmanager.query); OneToast.maketext ( This, query, toastlength.short); A } - } -}
2, write this on the Activity to use Search Dialog:
1 [MetaData ( " android.app.default_searchable ", value = Aa.searcha )] 2 [Activity (Label = search Span style= "color: #800000;" > ", Mainlauncher = true , Icon = " Span style= "color: #800000;" > @drawable/icon " )] 3 public class mainactivity: Activity {
the Activity on searchactivity must specify name, and must be the structure of xxx.xxx.xxx, if only write xxx, compile will error: The package name is missing.
The package name must start with a lowercase letter, or it will get an error when packaged: Value is specified in MetaData of the Activity that needs to use Search Dialog (mainactivity in the example above) Searchactiv ity name (aa.searcha) fee This name is added because: http://developer.xamarin.com/guides/android/advanced_topics/working_with_ androidmanifest.xml/
Activity Name
Beginning with Xamarin.android 5.1, the type name of a activity is based on the md5sum of the assembly-qualified name of The type being exported. This allows the same fully-qualified name to being provided from both different assemblies and not get a packaging error. (Before Xamarin.android 5.1, the default type name of the activity is created from the Lowercased namespace and the class Name.)
If you wish to override this default and explicitly specify the name of the your activity with the property Name
:
After Xamarin.android 5.1, the name of the activity will be handled by md5sum, like this:
<android:icon= "@drawable/icon" android:label= "Search" Android:name= "md54b7a0ef5c9d7075d66a4a3ca71919313. Mainactivity ">?
After compiling, you can view the final results of the obj\debug\android\androidmanifest.xml. How to get input search content
To use search Dialog visible, you need to trigger on Activity with search Dialog enabled: Onsearchrequested method.
1 Private void Btn_click (object sender, EventArgs e) {2this . Onsearchrequested (); 3 }
Some Android, will bring a search button, click this button, the same will call this onsearchrequested. When the user performs a search, the system creates a query that Intent stores the user.
In the Searchactivity,
1 if (this.) Intent.Action.Equals (Intent.actionsearch)) {2 varthis. Intent.getstringextra (searchmanager.query); 3 Toast.maketext (This, query, toastlength.short); 4 }
If you want to pass an extra parameter to searchactivity, you can pass it this way:
1 Public Override BOOLonsearchrequested () {2 varbundle =NewBundle ();3Bundle. Putboolean ("Key1",true);4 This. Startsearch (NULL,false, Bundles,false);5 return true;6}
Get extra parameters like this:
1 varbundle =Intent. Getbundleextra (searchmanager.appdata);2 if(Bundle! =NULL) {3 varKey1 = bundle. Getboolean ("Key1");4 varKey2 = bundle. Getboolean ("Key2");5}
-------------
OK, Finish.
Thanks, onlookers.
Xamarin Android search box: Searching Dialog