第一行Kotlin系列(一)kotlin按钮点击事件
按钮findViewBuId
<Button
android:id="@+id/mButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mButton3" />
XML 没有变化
val button4 = findViewById<Button>(R.id.mButton4)
点击事件有三种写法
1.匿名内部类
button4.setOnClickListener {
Toast.makeText(this, "java", Toast.LENGTH_LONG).show()
}
这里就使用Toast打印一句话,Toast的写法和java中的写法一样
2.Activity实现全局OnClickListener接口
class MainActivity : AppCompatActivity(), View.OnClickListener { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) initView()
}
在 AppCompatActivity类后加上 View.OnClickListener 用“,”分割,这种方法与java的区别是没有implements关键字表示实现接口。
private fun initView() {
val button1 = findViewById<Button>(R.id.mButton1)
val button2 = findViewById<Button>(R.id.mButton2)
val button4 = findViewById<Button>(R.id.mButton4)
val button5 = findViewById<Button>(R.id.mButton5)
val button6 = findViewById<Button>(R.id.mButton6)
val button7 = findViewById<Button>(R.id.mButton7) button1.setOnClickListener(this)
button2.setOnClickListener(this)
button7.setOnClickListener(this)
override fun onClick(v: View?) {
when (v?.id) {
R.id.mButton1 ->
Toast.makeText(this, "java", Toast.LENGTH_LONG).show()
R.id.mButton2 ->
Toast.makeText(this, "java", Toast.LENGTH_LONG).show()
}
}
在kotlin中使用when替代了java中的switch,“:”符号改为了“->”。
3.指定onClick属性
<Button
android:id="@+id/mButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mButton3"
android:text="关闭"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mButton2" />
fun mButton3(view: View) {
if (view.id == R.id.mButton3) {
finish()
}
}
代码中就实现了关闭当前Activity
以上
第一行Kotlin系列(一)kotlin按钮点击事件的更多相关文章
- js触发按钮点击事件
js触发按钮点击事件 博客分类: javascript 模拟JS触发按钮点击功能 <html> <head> <title>usually function&l ...
- 按钮点击事件,打开新的Activity
按钮点击事件,打开新Activity, 打开网页 findViewById(R.id.btnStartBAty).setOnClickListener(new View.OnClickListener ...
- Objective-C:模拟按钮点击事件理解代理模式
OC中的协议(Protocol)和和.NET中的接口(Interface)类似,简单来讲,就是一系列方法的列表,其中声明的方法可以被任何类实现.不同的是,在.NET中,如果某个类实现了一个接口,就必须 ...
- Javascript之三种按钮点击事件
学习Javascript必须要先掌握基本的事件方法和语法,这些都是我们学过的也是最基本的.以前忘了总结,所以现在回顾,综合地总结一下,温故而知新. Javascript有三种按钮点击事件,分别为ale ...
- 【转】Android开发20——单个监听器监听多个按钮点击事件
原文网址:http://woshixy.blog.51cto.com/5637578/1093936 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律 ...
- 【Android】按钮点击事件的常用写法
学习总结: 最近学习了Android点击事件的常用写法.点击事件会触发监听对象身上的回调,常用写法有以下四种: 方法一:使用匿名内部类. public class MainActivity exten ...
- ButtonAddListener监听按钮点击事件
ButtonAddListener监听按钮点击事件 using UnityEngine; using System.Collections; using UnityEngine.UI; using U ...
- js和jquery触发按钮点击事件
js触发按钮点击事件 function load(){ //下面两种方法效果是一样的 document.getElementById("target").onclick(); do ...
- Android Listview中Button按钮点击事件冲突解决办法
今天做项目时,ListView中含有了Button组件,心里一早就知道肯定会有冲突,因为以前就遇到过,并解决过,可惜当时没有记录下来. 今天在做的时候,继续被这个问题郁闷了一把,后来解决后,赶紧来记录 ...
- 查找和定位Android应用的按钮点击事件的代码位置基于Xposed Hook实现
本文博客地址:https://blog.csdn.net/QQ1084283172/article/details/80956455 在进行Android程序的逆向分析的时候,经常需要通过Androi ...
随机推荐
- LCA Nearest Common Ancestors (很典型的例题)
A rooted tree is a well-known data structure in computer science and engineering. An example is show ...
- curl book
curl是利用URL语法在命令行方式下工作的开源文件传输工具.它被广泛应用在Unix.多种Linux发行版中,并且有DOS和Win32.Win64下的移植版本. 官方地址 man page 下载页面, ...
- 用long类型让我出了次生产事故,写代码还是要小心点
昨天发现线上试跑期的一个程序挂了,平时都跑的好好的,查了下日志是因为昨天运营跑了一家美妆top级淘品牌店,会员量近千万,一下子就把128G的内存给爆了,当时并行跑了二个任务,没辙先速写一段代码限流,后 ...
- 箭头函数的this指向问题-一看就懂
OK,对于箭头函数的this 用一句话概括:箭头函数中的this指向的是定义时的this,而不是执行时的this. 如果上面这句话听的是懂非懂或者完全不懂的,没关系,下面会有案例讲解. 举个栗子 来看 ...
- IntelliJ IDEA在mac中完全删除方法
cd /Applications/ rm -r IntelliJ\ IDEA.app/ rm -r /Users/apple/Library/Logs/IntelliJIdea2019.3/ rm - ...
- 日志分析工具ELK(三)
目前官网更新特别快,不到半年时间就更新了好几个版本,目前最新的是5.1 以下安装配置使用4.5版本的 https://www.elastic.co/guide/en/kibana/4.5/index. ...
- 【集群实战】NFS网络文件共享服务3-相关知识补充(showmount,exports,rpc)
1. showmount命令说明 showmount命令一般用于从NFS客户端检查NFS服务器端共享目录的情况. 参数说明: -e,--exports 显示NFS服务器输出的目录列表 [root@we ...
- Mina Basics 02-基础
基础 在第1章中,我们简要介绍了Apache MINA.在本章中,我们将了解客户端/服务器体系结构以及有关基于MINA的服务器和客户端的详细信息. 我们还将基于TCP和UDP公开一些非常简单的服务器和 ...
- MySQL Change Data Directory
为什么80%的码农都做不了架构师?>>> Stop MySQL using the following command: sudo /etc/init.d/mysql stop ...
- nat和静态映射
拓扑图: 实验要求: 1.R2.R3能访问外网的4.4.4.4(4.4.4.4为R4上的环回接口,用来模拟inter网). 2.R4访问222.222.222.100其实访问到的是内网的192.168 ...