Android Button是用于在单击时执行事件的按钮。它是android.widget.Button类下的一个UI组件。要了解有关Android Button的更多信息, 请参阅Android Button示例

使用Kotlin, 我们可以使用以下方法通过不同的方式在Android Button上执行事件:

1.实现Button的setOnClickListener

button1.setOnClickListener(){

Toast.makeText(this, "button 1 clicked", Toast.LENGTH_SHORT).show()

}

2.实现View.OnClickListner并覆盖其功能

button2.setOnClickListener(this)

. .

override fun onClick(view: View) {

// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

}

3.在布局文件中添加Button的onClick属性并实现其功能。

android:onClick="clickButton"/>

fun clickButton(v: View){

val mToast = Toast.makeText(applicationContext, "button 3 clicked", Toast.LENGTH_SHORT)

mToast.show()

}

4.以编程方式创建一个Button并将其设置在布局上

button4.setLayoutParams(ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))

button4.setId(button4_Id)

button4.x = 250f

button4.y = 500f

button4.setOnClickListener(this)

constraintLayout.addView(button4)

Kotlin Android按钮示例

在此示例中, 我们将创建Button并对其执行事件。单击按钮, 显示一条祝酒消息。

activity_main.xml

在activity_main.xml布局文件中的Widgets面板中添加三个Button。其代码如下。 id button3的Button添加了onClick属性, 其函数名称在MainActivity类文件中实现。

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:id="@+id/constraintLayout"

tools:context="example.srcmini.com.kotlinbutton.MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button Action Example"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.073"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>

android:id="@+id/button1"

android:layout_width="95dp"

android:layout_height="wrap_content"

android:layout_marginBottom="8dp"

android:layout_marginEnd="8dp"

android:layout_marginStart="8dp"

android:layout_marginTop="8dp"

android:text="Button 1"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.501"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.498" />

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginBottom="80dp"

android:layout_marginEnd="8dp"

android:layout_marginStart="8dp"

android:layout_marginTop="8dp"

android:text="Button 2"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.762" />

android:id="@+id/button3"

android:layout_width="101dp"

android:layout_height="48dp"

android:layout_marginBottom="8dp"

android:layout_marginEnd="8dp"

android:layout_marginStart="8dp"

android:layout_marginTop="8dp"

android:onClick="clickButton"

android:text="Button 3"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.502"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.774" />

MainActivity.kt

在MainActivity.kt类中添加以下代码。在此类中, 我们在按钮上实现setOnClickListener侦听器, 实现View类(View.OnClickListener)的OnClickListener并覆盖其功能onClick。在该类中, 我们还以编程方式创建一个Button(button4), 定义其属性并将其设置在布局上。

package example.srcmini.com.kotlinbutton

import android.support.v7.app.AppCompatActivity

import android.os.Bundle

import android.view.View

import android.view.ViewGroup

import android.widget.Button

import android.widget.Toast

import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() , View.OnClickListener {

val button4_Id: Int = 1111

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

button1.setOnClickListener(){

Toast.makeText(this, "button 1 clicked", Toast.LENGTH_SHORT).show()

}

button2.setOnClickListener(this)

// add button dynamically

val button4 = Button(this)

button4.setText("Button 4 added dynamically")

button4.setLayoutParams(ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))

button4.setId(button4_Id)

button4.x = 250f

button4.y = 500f

button4.setOnClickListener(this)

constraintLayout.addView(button4)

}

override fun onClick(view: View) {

// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

when (view.id) {

R.id.button2 ->

Toast.makeText(this, "button 2 clicked", Toast.LENGTH_SHORT).show()//single line code

button4_Id->{//multiline code

val myToast = Toast.makeText(this, "button 4 clicked", Toast.LENGTH_SHORT)

myToast.show()

}

}

}

fun clickButton(v: View){

val mToast = Toast.makeText(applicationContext, "button 3 clicked", Toast.LENGTH_SHORT)

mToast.show()

}

}

输出:

kotlin android获取按钮,Kotlin Android按钮相关推荐

  1. android 获取对话框对象,Android 基本Dialog和自定义Dialog

    Android 基本Dialog和自定义Dialog Dialog类是对话框的基类,但你应该避免直接实例化Dialog ,可以使用子类 1.AlertDialog 此对话框可以显示标题,最多三个按钮, ...

  2. android获取activity截图,Android Activity 不能被截屏的解决方法

    Android Activity 不能被截屏的解决方法 在Activity 添加即可 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECU ...

  3. android获取图片缩略图,Android系获取图片和视频的缩略图

    获取手机里视频缩略图: public static Bitmap getVideoThumbnail(ContentResolver cr,Uri uri) { Bitmap bitmap = nul ...

  4. Android获取Linux图像信息,Android系统信息获取 之十三:Linux内核版本信息获取

    Android系统信息获取 之十三:Linux内核版本信息获取 Android系统是基于Linux的,各个Android版本对应的Linux版本不尽相同,我们这里不去追究各个Android对应的Lin ...

  5. android获取程序名称,Android获取应用程序名称(ApplicationName)示例

    MainActivity如下: 代码如下: package cn.testapplicationname; import android.os.Bundle; import android.widge ...

  6. android获取详细地址,Android获取当前子网掩码地址(亲测可用)

    Android 获取当前子网掩码地址(亲测可用),现在网上好多都是通过 DhcpInfo 来获取,但是通过这种方法有 Bug,很多人用 DhcpInfo 的方式都是获取不到,都是为 0.0.0.0. ...

  7. android获取apk版本号,android 获取apk的版本信息

    释放双眼,带上耳机,听听看~! 今天,简单讲讲android如何获取apk的版本信息. 这个很简单,但是之前还是查找了资料,所以记录一下. 一.应用程序得到自己的版本信息 /** * 得到当前应用版本 ...

  8. android 获取权限管理,Android获取超级管理员权限的实现

    Android获取超级管理员权限的实现 发布时间:2020-10-14 18:54:35 来源:脚本之家 阅读:86 作者:柚子君. 1.定义特殊的广播接收者,系统超级管理员的广播接收者 public ...

  9. android获取键盘状态,Android获取屏幕方向及键盘状态的小例子

    Android获取屏幕方向及键盘状态的小例子 复制代码 代码如下: Configuration config = getResources().getConfiguration(); if (conf ...

  10. android获取网络图片方法,Android获取网络图片并显示的方法

    本文实例为大家分享了Android获取网络图片并显示的具体代码,供大家参考,具体内容如下 使用 HttpURLConnection 获得连接,再使用 InputStream 获得图片的数据流,通过 B ...

最新文章

  1. [转](不理想)Ubuntu下更改主显示器
  2. sgmllib Introduction
  3. 写一个实时监控IP连通性的小脚本
  4. Mozilla 构建系统(转)
  5. WeekHashMap
  6. [转载] java:比较运算符
  7. Vue首页加载过慢 解决方案
  8. (10)机器学习_K邻近算法
  9. delphi 剪切板变量_delphi读写剪贴板
  10. 代码保护软件 VMProtect 用户手册: 什么是VMProtect?
  11. QTP10.0下载及安装说明
  12. java成员变量注释规范_java编程规范之java注释规范
  13. 计算机类毕业论文中期检查,计算机类毕业论文中期检查表
  14. win10c盘android,Win10系统C盘哪些文件可以删除?C盘无用文件都在哪?
  15. Windows中MSOCache文件夹
  16. ppt技巧一四步法调整段落排版
  17. python计算图形面积的方法,python实现计算图形面积
  18. 【opencv14】cv::Mat---Desne数组类
  19. 激发数据潜力,“东数西算”带动数据中心新发展
  20. 《Python数据分析基础教程:NumPy学习指南(第2版)》笔记5:第三章 常用函数1——文件读写、算术平均值、最大值最小值、极值

热门文章

  1. Python-初体验
  2. 验证GridControl Gridview 单元格。
  3. 计算机图形学中的边标志算法c++程序实现2
  4. Ubuntu 14.04 下每次重启系统都默认最大亮度的解决办法
  5. 周报_2013第02周(2013/01/06-2013/01/12)
  6. 标准 mysql 数据库 jdbc 的两种写法 懒汉式 和 饿汉式
  7. 【转帖】WEB架构师成长之路之一-走正确的路
  8. Win能ping通win7,但是无法访问共享的解决方法
  9. HBase完全分布式集群部署
  10. Zabbix自带模板监控MySQL服务