织梦网站栏目,福建建设网站,学院网站建设推进会,企业首页html源码#xff08;一#xff09;原本java的写法#xff08;相信很多是学过java的#xff09;#xff1a; 需要实现接口View.IOnClickListener#xff0c;最好也继承类#xff1a;Activity#xff0c;因为View.IOnClickListener接口又继承了IJavaObject, IDisposable接口…一原本java的写法相信很多是学过java的 需要实现接口View.IOnClickListener最好也继承类Activity因为View.IOnClickListener接口又继承了IJavaObject, IDisposable接口所以还学要实现这两个接口里面的成员而Activity已经实现可了这两个接口的成员就不需要我们再写了毕竟我们大部分只想重写View.IOnClickListener里面的OnClick函数。如果自定义的类只是实现了View.IOnClickListener接口不继承Activity就需要实现另外2个接口的其他成员在vs中选中接口使用快捷键AltShiftF10可快速实现接口 代码如下 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 FindViewByIdButton(Resource.Id.MyButton);
31 button.SetOnClickListener(this);
32
33 }
34 }
35 } 当然也可以自己定义一个类实现接口重写OnClick函数然后button.SetOnClickListener(你自定义类的实例); 二接下来介绍C#的4种写法其实大同小异 1.第一种创建模板就有的 1 Button button FindViewByIdButton(Resource.Id.MyButton);
2 button.Click delegate { button.Text string.Format ({0} clicks!, count);}; 2.第二种 1 namespace App22 {3 [Activity(Label App2, MainLauncher true, Icon drawable/icon)]4 5 6 public class MainActivity : Activity, View.IOnClickListener7 {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 FindViewByIdButton(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.IOnClickListener2 {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 FindViewByIdButton(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 } View Code 4.第四种 1 namespace App22 {3 [Activity(Label App2, MainLauncher true, Icon drawable/icon)]4 5 6 public class MainActivity : Activity, View.IOnClickListener7 {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 FindViewByIdButton(Resource.Id.MyButton);
21 button.Click (sender, e) { button.Text string.Format ({0} clicks!, count);};
22
23 }
24 }
25 } View Code 转载于:https://www.cnblogs.com/nightswatch/p/4276840.html