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

    }

相关推荐
CCTV果冻爽1 小时前
Android 源码集成可卸载 APP
android
秋月霜风2 小时前
mariadb主从配置步骤
android·adb·mariadb
Python私教3 小时前
Python ORM 框架 SQLModel 快速入门教程
android·java·python
编程乐学4 小时前
基于Android Studio 蜜雪冰城(奶茶饮品点餐)—原创
android·gitee·android studio·大作业·安卓课设·奶茶点餐
problc5 小时前
Android中的引用类型:Weak Reference, Soft Reference, Phantom Reference 和 WeakHashMap
android
IH_LZH5 小时前
Broadcast:Android中实现组件及进程间通信
android·java·android studio·broadcast
去看全世界的云5 小时前
【Android】Handler用法及原理解析
android·java
机器之心6 小时前
o1 带火的 CoT 到底行不行?新论文引发了论战
android·人工智能
机器之心6 小时前
从架构、工艺到能效表现,全面了解 LLM 硬件加速,这篇综述就够了
android·人工智能
AntDreamer6 小时前
在实际开发中,如何根据项目需求调整 RecyclerView 的缓存策略?
android·java·缓存·面试·性能优化·kotlin