Botton进一步了解(点击事件)

点击事件和长按事件

  • 监听器:专门监听控件的动作行为。只有控件发生了指定的动作,监听器才会触发开关区执行对应的代码逻辑。
  • 按钮控件有两种常用的监听器:
    • 点击监听器:通过setOnClickListener方法设置。按钮被按住少于500ms时会触发点击事件。
    • 长按监听器:通过setOnLongClickListener方法设置。按钮被按住超过500ms时,会触发长按事件。

点击事件

只有一个按钮
  • 示例代码
package com.example.chapter03

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.example.chapter03.util.DateUtil

class ButtonClickActivity : AppCompatActivity() {

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_button_click)
        val tv_result: TextView = findViewById(R.id.tv_result)
        val btn_click_single: Button = findViewById(R.id.btn_click_single)
        btn_click_single.setOnClickListener(MyOnClickListener(tv_result))    // 实例化类的一个对象出来

    }

    // 创建一个类 MyOnClickListener ,来实现 View.OnClickListener 接口
    class MyOnClickListener(private val tv_result: TextView) : View.OnClickListener {

        override fun onClick(p0: View?) {
            val dsec = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(), (p0 as Button).text)
            tv_result.text = dsec  // kotlin不用set(),也不用get()
        }

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_click_single"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="指定单独的点击监听器"
        android:textColor="#000000"
        android:textSize="15sp"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="15sp"
        android:text="查看按钮点击的结果"/>
</LinearLayout>
  • 运行结果
    在这里插入图片描述

(穿插kotlin小笔记)
(构造器传参 简写)

class MyOnClickListener: View.OnClickListener {
        private val tv_result: TextView

        constructor(tv_result: TextView) {
            this.tv_result = tv_result
        }
}

// 改进
class MyOnClickListener(private val tv_result: TextView) : View.OnClickListener {}
有两个按钮
  • 示例代码
package com.example.chapter03

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.example.chapter03.util.DateUtil

class ButtonClickActivity : AppCompatActivity() {

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_button_click)
        val tv_result: TextView = findViewById(R.id.tv_result)
        
        val btn_click_single: Button = findViewById(R.id.btn_click_single)
        btn_click_single.setOnClickListener(MyOnClickListener(tv_result))    // 实例化类的一个对象出来
        val btn_click_public: Button = findViewById(R.id.btn_click_public)
        btn_click_public.setOnClickListener(MyOnClickListener(tv_result))
    }

    // 创建一个类 MyOnClickListener ,来实现 View.OnClickListener 接口
    class MyOnClickListener(private val tv_result: TextView) : View.OnClickListener {
        override fun onClick(p0: View?) {
            val dsec = String.format("%s 您点击了按钮: %s", DateUtil.getNowTime(), (p0 as Button).text)
            tv_result.text = dsec  // kotlin不用set(),也不用get()
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_click_single"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="指定单独的点击监听器"
        android:textColor="#000000"
        android:textSize="15sp"/>

    <Button
        android:id="@+id/btn_click_public"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="指定公共的点击监听器"
        android:textColor="#000000"
        android:textSize="15sp"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="15sp"
        android:text="查看按钮点击的结果"/>

</LinearLayout>
  • 结果
    在这里插入图片描述
  • 23
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值