Android Button点击事件

news/2024/5/17 15:16:02 标签: android, gitee

一.Button点击事件

<!-- activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

    <Button
        android:id="@+id/button_one"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:background="@drawable/ic_btn_seletor"
        android:text="按钮" />
    <Button
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:id="@+id/button_two"
        android:text="按钮"/>
</LinearLayout>

 

 Button事件常见的时间有,点击事件,长按事件,触摸事件等,这里就不仔细介绍业务,只介绍两种实现方式:

  1. 获取按钮,使用按钮绑定事件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

    <Button
        android:id="@+id/button_one"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:text="按钮" />
</LinearLayout>
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = findViewById(R.id.button_one);

        /** 点击事件 */
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO 点击动作  触发事件后,会回调该方法
            }
        });

        /** 长按事件 */
        btn.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                // TODO 长按动作 触发事件后,会回调该方法
                return false; // 返回值为true,则会屏蔽点击事件(不再回调点击事件方法)。返回false,则会调用点击事件
            }
        });

        /** 触摸事件 */
        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                // TODO 触摸动作 触发事件后,会回调该方法
                // 触摸事件分为三种,
                return false; // 返回值为true,则会屏蔽点击事件和长按事件。返回false,则不会屏蔽
            }
        });
    }
}
  1. 在layout直接调用方法。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#FFFFFF"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

    <Button
        android:id="@+id/button_one"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:onClick="onClickListener"
        android:text="按钮" />

</LinearLayout>
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    public void onClickListener(View view) {
        // TODO 点击事件。这边就是相当于方法一中的public void onClick(View view);
    }
}


http://www.niftyadmin.cn/n/5429186.html

相关文章

java maven聚合项目-子项目重写父项目的依赖版本无法生效

前言 项目的springboot版本2.6.13 项目主pom 引入了springboot的依赖配置 <dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><…

软考77-上午题-【面向对象技术3-设计模式】-创建型设计模式02

一、生成器模式 1-1、意图 将一个复杂对象的构建与它的表示分离&#xff0c;使得同样的构建过程可以创建不同的表示。 1-2、结构图 Builder 为创建一个 Product 对象的各个部件指定抽象接口。ConcreteBuilder 实现 Builder 的接口以构造和装配该产品的各个部件&#xff0c;定…

java kafka客户端何时设置的kafka消费者默认值

kafka为什么有些属性没有配置却能正常工作&#xff0c;那是因为kafka-clients对有些消费者设置了默认值&#xff0c;具体看下ConsumerConfig类的静态模块&#xff0c;具体如下所示&#xff1a; kafka为什么有些属性没有配置却能正常工作&#xff0c;那是因为kafka-clients对有…

linux系统安装 -----Centos 8 64位

创建新的虚拟机 选择自定义&#xff0c;点击下一步 默认点击下一步 选择稍后安装操作系统&#xff0c;点击下一步 选择虚拟机型号&#xff0c;点击下一步 选择文件所安装的位置&#xff0c;点击下一步 添加处理器配置&#xff0c;默认就好&#xff0c;视情况而定&#xff0c;点…

Excel小技巧 (3) - 如何取整

1. 四舍五入 Round&#xff08;对象&#xff0c;小数点后位数&#xff09; 结果 123.1 2.向上取整 Roundup&#xff08;对象&#xff0c;小数点后位数&#xff09; 结果&#xff1a;123.2 3.向下取整 Round&#xff08;对象&#xff0c;小数点后位数&#xff09; 结果123.…

什麼是HTTPS正向代理?

"HTTPS正向代理"由兩部分組成&#xff0c;“HTTPS”和“正向代理”。“HTTPS”指的是超文本傳輸安全協議&#xff0c;而“正向代理”指的是代理伺服器的一種類型。下麵我們分別解釋一下這兩個部分。 首先&#xff0c;“HTTPS”指的是超文本傳輸安全協議&#xff0c;…

vr虚拟现实游戏世界介绍|数字文化展览|VR元宇宙文旅

虚拟现实&#xff08;VR&#xff09;游戏世界是一种通过虚拟现实技术创建的沉浸式游戏体验&#xff0c;玩家可以穿上VR头显&#xff0c;仿佛置身于游戏中的虚拟世界中。这种技术让玩家能够全方位、身临其境地体验游戏&#xff0c;与游戏中的环境、角色和物体互动。 在虚拟现实游…

ArcGIS全系列实战视频教程——9个单一课程组合+系列直播回放

《ArcGIS全系列实战视频教程》是由9个单一课程组合合成。组成一条ArcGIS入门实战各项专题深入应用学习全链条&#xff0c;让你学有方向、学有目的&#xff0c;系统全面掌握ArcGIS。 ArcGIS全系列实战视频教程——9个单一课程组合https://edu.csdn.net/combo/detail/2569 《Ar…