在 axml 布局中声明的按钮处理程序
Handler for Button Declared in axml Layout
我猜这很容易,但似乎无法搞定。
我想在 axml 文件中定义一个按钮,然后 link 单击一个处理程序。我知道我可以使用 FindViewById<Button>(Resource.Id.Button2).Click += buttonClick;
手动设置处理程序,但我想知道是否有必要这样做。
如何让处理程序受 axml 文件中 android:onClick="buttonClick"
的约束?抛出的错误是
Could not find a method buttonClick(View) in the activity class md....Activity1 for onClick handler on view class android.widget.Button with id 'Button1'
[Activity (Label = "SlidingMenuExample", MainLauncher = true)]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView (Resource.Layout.menu);
}
public void buttonClick(object sender, EventArgs e) {
}
// This seems similar to the Java approaches. Why doesn't it work?
//public void buttonClick(View view) {
//}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/Button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button 2"
android:onClick="buttonClick" />
</LinearLayout>
谢谢。
当然可以。您需要添加对 Mono.Android.Export
.
的引用
然后你需要像这样注释你的方法:
[Export("buttonClick")]
private void ButtonClick(View view)
{
}
如果您不注释该方法,Java 世界将不会在 Activity 中知道您的方法。
我猜这很容易,但似乎无法搞定。
我想在 axml 文件中定义一个按钮,然后 link 单击一个处理程序。我知道我可以使用 FindViewById<Button>(Resource.Id.Button2).Click += buttonClick;
手动设置处理程序,但我想知道是否有必要这样做。
如何让处理程序受 axml 文件中 android:onClick="buttonClick"
的约束?抛出的错误是
Could not find a method buttonClick(View) in the activity class md....Activity1 for onClick handler on view class android.widget.Button with id 'Button1'
[Activity (Label = "SlidingMenuExample", MainLauncher = true)]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView (Resource.Layout.menu);
}
public void buttonClick(object sender, EventArgs e) {
}
// This seems similar to the Java approaches. Why doesn't it work?
//public void buttonClick(View view) {
//}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/Button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button 2"
android:onClick="buttonClick" />
</LinearLayout>
谢谢。
当然可以。您需要添加对 Mono.Android.Export
.
然后你需要像这样注释你的方法:
[Export("buttonClick")]
private void ButtonClick(View view)
{
}
如果您不注释该方法,Java 世界将不会在 Activity 中知道您的方法。