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 }
Mono for android,Xamarin点击事件的多种写法的更多相关文章
- Android笔记——Button点击事件几种写法
Button点击事件:大概可以分为以下几种: 匿名内部类 定义内部类,实现OnClickListener接口 定义的构造方法 用Activity实现OnClickListener接口 指定Button ...
- Android学习-----Button点击事件几种写法
Button点击事件:大概可以分为以下几种: 匿名内部类 定义内部类,实现OnClickListener接口 定义的构造方法 用Activity实现OnClickListener接口 指定Button ...
- 【Android】按钮点击事件的常用写法
学习总结: 最近学习了Android点击事件的常用写法.点击事件会触发监听对象身上的回调,常用写法有以下四种: 方法一:使用匿名内部类. public class MainActivity exten ...
- Android journey3 @点击事件的4种写法
对于android布局中的控件,如Button等会有相应的点击事件去响应它所需要的功能,今天我们就以电话拨号器的代码说明下几种点击事件: package com.itheima.phone; impo ...
- [Android]Java中点击事件的四种写法
点击事件的必备条件:实现OnClickListener接口,重写onclick(View v)方法 以拨号简单案例为例,如下图效果: 逻辑流程: 获取点击对象,获取数据 给对象设置监听类 实现OnCl ...
- Android——自定义多击事件
一:使用场景 Android本身内置了点击.双击事件,但是某些时候,我们可能需要多击事件. 例如:某个秘密入口,为了避免用户误操作点击.双击到了触发开关而进入到不该被用户看到的页面,我们可以为入口控件 ...
- Android笔记---点击事件的四种写法
Android 点击事件的四种写法: 1. 以内部类的形式实现 OnClickListener 接口.定义点击事件 class MainActivity extents Activity{ // .. ...
- Android 为点击事件添加震动效果
Android 点击Button 实现震动效果 学习自:网络 Overview 在Android 的点击效果中,遇到震动效果的还是很多的. 接下来就让我们看一下如何实现震动效果. 所需要的权限 如果我 ...
- Android实现点击事件的4种方式
一.通过在activity_main.xml中,按钮button控件中添加onclick事件实现 在 activity_main.xml 对应的按钮Button中加入下面红色事件 <Butt ...
随机推荐
- Linux学习之iostat命令详解
我们可以用iostat 命令来监视系统输入/输出.设备负载,这通过观察与它们的平均传送速率相关的物理磁盘的活动时间 来实现.iostat 命令生成的报告可以用来更改系统配置,从而更好地平衡物理磁盘和适 ...
- c#如何取出指定的中间文本
///<summary> ///取出文本中间内容 ///<summary> ///<param name="left">左边文本</par ...
- 2. Spring早期类型转换,基于PropertyEditor实现
青年时种下什么,老年时就收获什么.关注公众号[BAT的乌托邦],有Spring技术栈.MyBatis.JVM.中间件等小而美的原创专栏供以免费学习.分享.成长,拒绝浅尝辄止.本文已被 https:// ...
- box-sizing什么时候用?常用的值都有什么?
一般在做自适应的网页设计的时候用,用这个属性网页结构才不会被破坏. 常用的值: 1. content-box:宽度和高度分别应用到元素的内容框,在宽度和高度之外绘制元素的内边距和边框. 2. bo ...
- 搭建docker registry私有镜像仓库
搭建docker registry私有镜像仓库 一.安装docker-distribution yum install -y docker-distribution 安装完成后,启动服务: syste ...
- Hexo博客框架10分钟搭建个人博客
首先是先给大家打个招呼 最近看网上看到了很多的的关于搭建博客的视频,我自己也学着自己搭建了一个博客"我自己的博客链接"(欢迎大家来我的博客跟我深入交♂流),今天我把搭建的过程记录下 ...
- 2020传智博黑马python课
网上花钱买来的资源,免费分享给冷冷的兄弟们! 已经把相关网站广告后缀名全部替换修改,现在文件已经全部没有广告了, 不过就是课件的压缩包需要密码,已经在该文件夹说明,大家放心使用, 文件清爽,文件名 ...
- Python 学习笔记 之 随着学习不断更新的Python特性搜集
大小写敏感 缩进敏感--tab和空格不要混用,最好使用4个空格进行缩进.可使用vim配置缩进字符为4个空格 编写py文件时注意文件的编码,UTF-8 without BOM, 并且记得声明coding
- centos 7.0下安装MySQL 5.7.26
1.下载MySQL 5.7.26安装包,卸载自带MySQL数据库 yum remove mariadb-libs -y yum install -y libaio-devel 2.上传MySQL 5. ...
- java.net.BindException:Problem binding to [hostname:8088]地址已在使用
异常提示端口号被占用 查找被占用的端口 netstat -tln netstat -tln | grep 8083 netstat -tln ## 查看端口使用情况,而netstat -tln | g ...