Mono for android,Xamarin點擊事件的多種寫法

來源:互聯網
上載者:User

標籤:

(一)原本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 App210 {11     [Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")]12 13 14     public class MainActivity : Activity, View.IOnClickListener15     {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 resource26             SetContentView(Resource.Layout.Main);27 28             // Get our button from the layout resource,29             // and attach an event to it30              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 resource16             SetContentView(Resource.Layout.Main);17 18             // Get our button from the layout resource,19             // and attach an event to it20              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 resource11             SetContentView(Resource.Layout.Main);12 13             // Get our button from the layout resource,14             // and attach an event to it15              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 Code

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 resource16             SetContentView(Resource.Layout.Main);17 18             // Get our button from the layout resource,19             // and attach an event to it20              button = FindViewById<Button>(Resource.Id.MyButton);21              button.Click += (sender, e) =>{ button.Text = string.Format ("{0} clicks!", count++);};22      23          }24         }25 }
View Code

 

Mono for android,Xamarin點擊事件的多種寫法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.