Mono for android,Xamarin点击事件的多种写法

(一)原本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-08-01 22:53:11

Mono for android,Xamarin点击事件的多种写法的相关文章

android 按钮点击事件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBo

Android笔记——Button点击事件几种写法

Button点击事件:大概可以分为以下几种: 匿名内部类 定义内部类,实现OnClickListener接口 定义的构造方法 用Activity实现OnClickListener接口 指定Button的onClick的属性 首先我们简单地定义一个带Button的xml布局文件 activity_main.xml: <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:

android HOME点击事件的获取

 首先声明我是做系统开发的(高通平台),所以下面介绍的方法并不适合应用开发者. 最经有个需求要屏蔽HOME按键返回桌面并且实现自己的功能,发现以前的方式报错用不了,上网搜索了一下,发现都是抄来抄去基本是无用的.网上的方法不外乎这几种: 第一, 大家最常用的重写onAttachedToWindow()方法,然后在HOME点击事件KeyEvent.KEYCODE_HOME中做自己想做的事情,但是这个方法google处于安全考虑在android2.3.3之后就不支持了. 第二, 抓取系统log日志,判

Android基础--点击事件的四种写法

1.定义内部类,实现点击事件,使用时将内部类对象传入事件源的setOnClickListener()方法中 private class MyClickListener implements View.OnClickListener{ @Override public void onClick(View v) { // 功能代码 } } 2.使用匿名内部类的方式实现点击事件 setOnClickListener(new View.OnClickListener() { @Override publ

android Button点击事件总结

直接上代码: public class MainActivity extends AppCompatActivity implements View.OnClickListener{ Button button1,button2,button3,button4,button5,button6; TextView text1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns

Android按钮点击事件

Android中Button的点击事件非常简单,主要是一个内部类的问题 在界面上存在两个按钮和一个文本框,点击不同按钮的时候文本框中显示不同按钮的文字信息 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="

归纳总结Android的点击事件

地址 http://blog.csdn.net/xiangyong_1521/article/details/78489254 目录 常见按钮点击 点击的其他方式 对话框按钮点击 列表点击 物理键点击 附 1. 常见按钮点击 a.单击事件,主要用于Button和ImageButton控件,布局视图与TextView.ImageView控件用的也比较多.相关类名与方法说明如下: 监听器类名 : View.OnClickListener 设置监听器的方法 : setOnClickListener 监

Android笔记---点击事件的四种写法

Android 点击事件的四种写法: 1. 以内部类的形式实现 OnClickListener 接口.定义点击事件 class MainActivity extents Activity{ // ... private class MyListener implements OnClickListener{ public void Onclick(View v){ // ... 点击事件的响应 } } } 2. 採用匿名内部类 ? ?缺点:若是点击事件太多,则须要的匿名内部类太多 class M

来,一起梳理下Android响应点击事件的方法

一.设置setOnClickListener 这应该是最原始的方法了吧,来,先上代码: 布局文件: 1 <Button 2 android:id="@+id/button1" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 android:layout_marginLeft="63dp" 6 android:la