Android Button点击事件

一.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,则不会屏蔽
             }
         });
     }

    }

  2. 在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);
     }

    }

相关推荐
zh_xuan41 分钟前
Android Looper源码阅读
android
用户02738518402611 小时前
[Android]RecycleView的item用法
android
前行的小黑炭12 小时前
Android :为APK注入“脂肪”,论Android垃圾代码在安全加固中的作用
android·kotlin
帅得不敢出门13 小时前
Docker安装Ubuntu搭建Android SDK编译环境
android·ubuntu·docker
tangweiguo0305198713 小时前
Android Kotlin 动态注册 Broadcast 的完整封装方案
android·kotlin
fatiaozhang952713 小时前
浪潮CD1000-移动云电脑-RK3528芯片-2+32G-安卓9-2种开启ADB ROOT刷机教程方法
android·网络·adb·电脑·电视盒子·刷机固件·机顶盒刷机
前行的小黑炭13 小时前
Android 不同构建模式下使用不同类的例子:如何在debug模式和release模式,让其使用不同的类呢?
android·kotlin·gradle
andyguo14 小时前
AI模型测评平台工程化实战十二讲(第一讲:从手工测试到系统化的觉醒)
android
2501_9159214314 小时前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
幂简集成15 小时前
通义灵码 AI 程序员低代码 API 课程实战教程
android·人工智能·深度学习·神经网络·低代码·rxjava