Xamarin Framework Development Android Search dialog Case Search

Source: Internet
Author: User
Tags lowercase

Android searches are available in two ways: Search Dialog,searchview.

Searchview simple, free to use, here is the basic use of Search Dialog, because Xamarin is slightly different from native Android.

Effect:



searchable

To use the search Dialog, you need to configure a searching profile: 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

<?xml version= "1.0" encoding= "Utf-8"?>
<searchable xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:label= "@string/searchlabel"
android:hint= "@string/searchplaceholder"
>
</searchable>

The root node must be searchable.

Android:label is required

Android:hint is the placeholder in HTML5.

Other generic options please refer to:

Http://developer.android.com/guide/topics/search/searchable-config.html

activity enable Search DIAOG feature

To enable search Dialog, you must also open searches on the activity

The manifest file can be modified directly in the native Android App.

As follows:

<activity android:name= "Search" >
<intent-filter>
<action android:name= "Android.intent.action.SEARCH"/>
</intent-filter>
<meta-data android:name= "android.app.searchable" android:resource= "@xml/searchable"/>
</activity>

<activity android:name= ". Mainactivity ">
<meta-data android:name= "Android.app.default_searchable"
Android:value= ". Search "/>
</activity>

Android:resource= "@xml/searchable" refers to the above configuration file (Xml/searchable.xml)

The yellow callout is a fixed string.

This section can refer to:

Http://developer.android.com/guide/topics/search/search-dialog.html

Under Xamarin, the manifest file is the androidmanifest.xml below the properties



This file is maintained by Xamarin and is not recommended for direct modification.

To open an activity search, you can do this:

1, create a new searchactivity

[MetaData ("android.app.searchable", Resource = "@xml/searchable")]
[Intentfilter (new[] {intent.actionsearch})]
[Activity (Label = "Searchactivity", name = "Aa.searcha")]//Aa.searcha, AA is the package name and must begin with a lowercase letter
public class Searchactivity:activity {
protected override void OnCreate (Bundle Bundle) {
Base. OnCreate (bundle);


if (this. Intent.Action.Equals (Intent.actionsearch)) {
var query = this. Intent.getstringextra (Searchmanager.query);
Toast.maketext (this, query, Toastlength.short);
}
}
}

2, write this on the activity that you want to use Search Dialog:

[MetaData ("android.app.default_searchable", Value = "Aa.searcha")]
[Activity (Label = "Search", Mainlauncher = true, Icon = "@drawable/icon")]
public class Mainactivity:activity {

The activity on searchactivity must specify name and must be a XXX.XXX.XXX structure, and if only write XXX, the compilation will report an error: The package name is missing.



The package name must start with a lowercase letter, or it will be packaged with an error:



Specify the Name (Aa.searcha) of Value searchactivity in the MetaData of the activity that needs to use Search Dialog (mainactivity in the example above)

The reason for this strength is that the Name is:

http://developer.xamarin.com/guides/android/advanced_topics/working_with_androidmanifest.xml/

Activity Name

Beginning with Xamarin.android 5.1, the type name of ' an ' is based on the md5sum of the assembly-qualified name of The type being exported. This allows the same fully-qualified name to is provided from two different and don't get a assemblies error. (Before Xamarin.android 5.1, the default type name of the activity is created from lowercased namespace and the class Name.)

If you wish to override this default and explicitly specify the name of your activity, use the Name property:

After Xamarin.android 5.1, the name of the activity will be handled by md5sum, like this:



After compiling, you can view the final results of the obj\debug\android\androidmanifest.xml.

How to get input search content

To be visible using search Dialog, you need to trigger on an activity that has the search Dialog enabled: Onsearchrequested method.

private void Btn_click (object sender, EventArgs e) {
This. Onsearchrequested ();
}

Some Android machines will bring a search button, click the button, and call the onsearchrequested.

When a user performs a search, the system creates a query that Intent stores the user.

In the Searchactivity,

if (this. Intent.Action.Equals (Intent.actionsearch)) {
var query = this. Intent.getstringextra (Searchmanager.query);
Toast.maketext (this, query, Toastlength.short);
}

If you want to pass additional parameters to searchactivity, you can pass it this way:

public override bool Onsearchrequested () {
var bundle = new Bundle ();
Bundle. Putboolean ("Key1", true);
This. Startsearch (null, FALSE, bundle, false);
return true;
}

Get extra parameters to do this:

var bundle = Intent. Getbundleextra (Searchmanager.appdata);
if (bundle!= null) {
var key1 = bundle. Getboolean ("Key1");
var Key2 = bundle. Getboolean ("Key2");
}



Android Search Framework Development


Android is Google's product, so it's a natural search. Take a look at the search dialog box in some Android apps.


Figure 1 Global search in Android


Figure 2 Contact Search


Figure 3 Music Search

These are all by pressing the search button on the Physical Keyboard pop-up Search dialog box, of course, search keyword hints are indispensable. How to achieve it?

First, configure the search description file

The XML file in res is created with the following contents: Sreachable.xml

<searchable xmlns:android= "http://schemas.android.com/apk/res/android"
android:label= "@string/search_ Label "
android:hint=" @string/search_hint "
android:searchsettingsdescription=" @string/settings_desc Ription ">

Second, create Searchablemusicactivity.java

At least you need to implement the OnCreate method to show it.

Third, the configuration Androidmanifest.xml

<?xml version= "1.0"  encoding= "Utf-8"?> <manifest xmlns:android= "http://" Schemas.android.com/apk/res/android "    package=" Com.halzhang.android.search " android : versioncode= "1"     android:versionname= "1.0" >     <application  android:icon= "@drawable/icon"  android:label= "@string/app_name" >          <activity android:name= ". Searchablemusicactivity "            android:label= "@string/app_name"  android:theme= "@android: Style/theme.notitlebar" >              <intent-filter>                 <action android:name= "Android.intent.action.MAIN"  />               &Nbsp;<category android:name= "Android.intent.category.LAUNCHER"  />             </intent-filter>             <intent-filter>             <!--  Configuration action -->                 <action android:name= "Android.intent.action.SEARCH"  />             </intent-filter>             <!--  Specify the configuration file for the search  -->             <meta-data android:name= "Android.app.searchable"                 android:resource= "@xml/searchable"  / >      &Nbsp; </activity>        <meta-data android:name= " Android.app.default_searchable "           android:value =". Searchablemusicactivity " />

Through the above three steps will be able to implement the Search dialog box.


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.