Use android:onclick= "xxx" property in Xamarin.android

Source: Internet
Author: User

In native Android development, there are three ways to add click events to a view;

1. Using Anonymous objects

((ImageButton) Findviewbyid (r.id.music_cover))
. Setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
}
});

We know that View.onclicklistener is an interface, interface is not directly instantiated, so the above is the use of the principle of anonymous objects to achieve;

2, the use of the implementation of the View.onclicklistener object

This approach is typically to instantiate an object that implements this interface in the View.setonclicklistener method,

Implement this interface first:

Class Myclicklistener implements view.onclicklistener{
@Override
public void OnClick (View v) {
}
}

Use:

((ImageButton) Findviewbyid (R.id.music_cover)). Setonclicklistener (New Myclicklistener ());


3. Using the Android:onclick property

Set the property android:onclick= "Onviewclick"in the view, which specifies the name of the function called when clicked

Implement this naming method in activity

public void Onviewclick (View v) {
Toast.maketext (This, "onclick", Toast.length_short). Show ();
}

The above is in Java to increase the view of the Click event method, but the specific use to understand these are not enough, there are a few points to note, the final summary;

The title is about how to use the Android:onclick attribute in xamarin.android, so this is the key, and incidentally, how to bind the view's Click event in Xamarin

There are three ways to achieve xamarin.android, but there is a certain difference from the original.

1. Use the view's Click event

View. Click + = xxxx

Because there is no anonymous object in C # this thing, but relative to provide C # event subscription this way, but this way of use, there is a lot of attention, C # event subscription use is not good, may cause multiple subscriptions, and even in extreme cases caused memory leaks, this piece has a lot of content to say, follow-up will continue to introduce the explanation;

2, this method is similar to the second method of Java

3. Consistent with Java's third approach, but used in Xamarin, you must introduce the Mono.Android.Export dynamic library

and label the [Java.Interop.Export] property on the appropriate method, such as the following

[Java.Interop.Export]
public void Onview_click (View v) {
}


Finally, two points to note when using the third method:

1, the method must be externally visible, what meaning, is the method of the outer layer can be directly accessed, the reason is very simple, XML is also used for parsing, this property is not

So divine, also by parsing processing, and then binding this method, so this method must be visible externally, simple words: public decoration, Bob

2, use this attribute view must be clickable, such as Button,imagebutton and so on itself is clickable, that TextView can use it? Sure you can, but be sure to

Set another property, android:clickable= "true".

Roughly to this end, for the advantages and disadvantages of these three ways and the use of the scene, there is time to say my own views. Novice posts, master do not spray!

Use android:onclick= "xxx" property in Xamarin.android

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.