Xamarin.Android: How to capture Button events defined in the OnClick XML attribute?

前端 未结 1 953
耶瑟儿~
耶瑟儿~ 2020-12-09 12:18

I have this Button inside a RelativeLayout which is included as part of a custom ListView row Layout.

相关标签:
1条回答
  • 2020-12-09 12:57

    Add the attribute [Java.Interop.Export] to your click handler method:

    [Java.Interop.Export("myClickHandler")] // The value found in android:onClick attribute.
    public void myClickHandler(View v) // Does not need to match value in above attribute.
    {
        Console.WriteLine ((v as Button).Text);
    }
    

    This will expose the method to Java via the Generated Callable Wrapper for your activity so it can invoked from the Java runtime.

    See:

    1. Mono For Android 4.2 release notes within the section titled "Exporting arbitrary Java member for better Java integration".

    Important Note

    Using [Java.Interop.Export] requires you to add the Mono.Android.Export assembly to your project.

    Therefore this feature is only available for Indie and higher licences.

    0 讨论(0)
提交回复
热议问题