(一)原本java的写法(相信很多是学过java的):
需要实现接口View.IOnClickListener,最好也继承类:Activity,因为View.IOnClickListener接口又继承了IJavaObject, IDisposable接口,所以还学要实现这两个接口里面的成员,而Activity已经实现可了这两个接口的成员,就不需要我们再写了,毕竟我们大部分只想重写View.IOnClickListener里面的OnClick函数。(如果自定义的类只是实现了View.IOnClickListener接口,不继承Activity,就需要实现另外2个接口的其他成员,在vs中选中接口,使用快捷键Alt+Shift+F10,可快速实现接口)
代码如下:
1 using System; 2 using Android.App; 3 using Android.Content; 4 using Android.Runtime; 5 using Android.Views; 6 using Android.Widget; 7 using Android.OS; 8 9 namespace App2 10 { 11 [Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")] 12 13 14 public class MainActivity : Activity, View.IOnClickListener 15 { 16 Button button; 17 public void OnClick(View v) 18 { 19 button.Text = string.Format("{0} clicks!", v.Id); 20 } 21 protected override void OnCreate(Bundle bundle) 22 { 23 base.OnCreate(bundle); 24 25 // Set our view from the "main" layout resource 26 SetContentView(Resource.Layout.Main); 27 28 // Get our button from the layout resource, 29 // and attach an event to it 30 button = FindViewById<Button>(Resource.Id.MyButton); 31 button.SetOnClickListener(this); 32 33 } 34 } 35 }
当然也可以自己定义一个类,实现接口,重写OnClick函数,然后button.SetOnClickListener(你自定义类的实例);
(二)接下来介绍C#的4种写法(其实大同小异)
1.第一种(创建模板就有的):
1 Button button = FindViewById<Button>(Resource.Id.MyButton); 2 button.Click += delegate { button.Text = string.Format ("{0} clicks!", count++);};
2.第二种
1 namespace App2 2 { 3 [Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")] 4 5 6 public class MainActivity : Activity, View.IOnClickListener 7 { 8 int count = 1; 9 Button button; 10 11 protected override void OnCreate(Bundle bundle) 12 { 13 base.OnCreate(bundle); 14 15 // Set our view from the "main" layout resource 16 SetContentView(Resource.Layout.Main); 17 18 // Get our button from the layout resource, 19 // and attach an event to it 20 button = FindViewById<Button>(Resource.Id.MyButton); 21 button.Click +=button_Click; 22 23 } 24 25 private void button_Click(object sender, EventArgs e) 26 { 27 button.Text = string.Format("{0} clicks!", count++); 28 } 29 30 31 } 32 }
3.第三种
1 public class MainActivity : Activity, View.IOnClickListener 2 { 3 int count = 1; 4 Button button; 5 6 protected override void OnCreate(Bundle bundle) 7 { 8 base.OnCreate(bundle); 9 10 // Set our view from the "main" layout resource 11 SetContentView(Resource.Layout.Main); 12 13 // Get our button from the layout resource, 14 // and attach an event to it 15 button = FindViewById<Button>(Resource.Id.MyButton); 16 button.Click += new EventHandler(button_Click); 17 18 } 19 20 private void button_Click(object sender, EventArgs e) 21 { 22 button.Text = string.Format("{0} clicks!", count++); 23 } 24 25 26 }
4.第四种
1 namespace App2 2 { 3 [Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")] 4 5 6 public class MainActivity : Activity, View.IOnClickListener 7 { 8 int count = 1; 9 Button button; 10 11 protected override void OnCreate(Bundle bundle) 12 { 13 base.OnCreate(bundle); 14 15 // Set our view from the "main" layout resource 16 SetContentView(Resource.Layout.Main); 17 18 // Get our button from the layout resource, 19 // and attach an event to it 20 button = FindViewById<Button>(Resource.Id.MyButton); 21 button.Click += (sender, e) =>{ button.Text = string.Format ("{0} clicks!", count++);}; 22 23 } 24 } 25 }
时间: 2024-10-04 01:39:41