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);
     }

    }

相关推荐
顾林海几秒前
Android MMKV 深度解析:原理、实践与源码剖析
android·面试·源码阅读
雨白40 分钟前
TCP/IP 核心概念详解:从网络分层到连接管理
android
Wgllss2 小时前
雷电雨效果:Kotlin+Compose+协程+Flow 实现天气UI
android·架构·android jetpack
用户207038619493 小时前
Compose 可点击文本:ClickableText Compose 中的 ClickableSpan
android
常利兵4 小时前
Kotlin作用域函数全解:run/with/apply/let/also与this/it的魔法对决
android·开发语言·kotlin
幼稚园的山代王4 小时前
Kotlin-基础语法练习一
android·开发语言·kotlin
闻不多4 小时前
用llamaindex搭建GAR遇到400
android·运维·服务器
阿华的代码王国4 小时前
【Android】适配器与外部事件的交互
android·xml·java·前端·后端·交互
跨界混迹车辆网的Android工程师5 小时前
实现Android图片手势缩放功能的完整自定义View方案,结合了多种手势交互功能
android·交互
wyjcxyyy5 小时前
打靶日记-PHPSerialize
android