Mono for android,Xamarin点击事件的多种写法
(一)原本java的寫法(相信很多是學(xué)過(guò)java的):
需要實(shí)現(xiàn)接口View.IOnClickListener,最好也繼承類:Activity,因?yàn)閂iew.IOnClickListener接口又繼承了IJavaObject, IDisposable接口,所以還學(xué)要實(shí)現(xiàn)這兩個(gè)接口里面的成員,而Activity已經(jīng)實(shí)現(xiàn)可了這兩個(gè)接口的成員,就不需要我們?cè)賹懥?#xff0c;畢竟我們大部分只想重寫View.IOnClickListener里面的OnClick函數(shù)。(如果自定義的類只是實(shí)現(xiàn)了View.IOnClickListener接口,不繼承Activity,就需要實(shí)現(xiàn)另外2個(gè)接口的其他成員,在vs中選中接口,使用快捷鍵Alt+Shift+F10,可快速實(shí)現(xiàn)接口)
代碼如下:
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 }當(dāng)然也可以自己定義一個(gè)類,實(shí)現(xiàn)接口,重寫OnClick函數(shù),然后button.SetOnClickListener(你自定義類的實(shí)例);
(二)接下來(lái)介紹C#的4種寫法(其實(shí)大同小異)
1.第一種(創(chuàng)建模板就有的):
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 } View Code4.第四種
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 } View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/nightswatch/p/4276840.html
總結(jié)
以上是生活随笔為你收集整理的Mono for android,Xamarin点击事件的多种写法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 图片上的文字怎么转换为word
- 下一篇: 第六章 Qt布局管理器Layout