Android 组件的四种点击事件写法

一:Android中点击事件的四种写法

  1. 通过布局属性实现点击功能
  2. 使用匿名内部类实现点击事件
  3. 使用内部类实现点击事件
  4. 让MainActivity实现View.OnClickListener接口

二:通过布局属性实现点击功能

1:.给Button控件添加onClick属性
【注意:该参数值为点击事件的方法名】
在这里插入图片描述
在这里插入图片描述

2: 创建点击事件方法
Alt+Enter键,选择Create …创建点击事件方法
在这里插入图片描述
编译器 自动在MainActivity.class中创建该方法
在这里插入图片描述

三:使用匿名内部类实现点击事件

在这里插入图片描述

四:使用内部类实现点击事件

1:创建一个btn3_onClickListener的内部类实现View.OnClickListener接口
选择Create inner class…选项
在这里插入图片描述
编译器自动生成该类
在这里插入图片描述
2:重写Onclick方法
【注意:选择Implement methods选项】
在这里插入图片描述
点击ok
在这里插入图片描述
编译器自动生成onClick方法
在这里插入图片描述
在onClick方法中实现具体业务
在这里插入图片描述

四:让MainActivity实现View.OnClickListener接口

1:MainActivity类实现View.OnClickListener接口
在这里插入图片描述
2:重写Onclick方法
选择Implement methods选项
在这里插入图片描述
点击ok
在这里插入图片描述

编译器自动生成onClick方法

在这里插入图片描述
在onClick方法中实现具体业务
在这里插入图片描述

五:项目完整代码

1:项目整体结构
在这里插入图片描述
2:前端布局代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:onClick="btn_onclick1"
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginBottom="48dp"
        android:text="第一种"
        app:layout_constraintBottom_toTopOf="@+id/btn_2"
        app:layout_constraintStart_toStartOf="@+id/btn_2" />

    <Button
        android:id="@+id/btn_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="160dp"
        android:layout_marginLeft="160dp"
        android:layout_marginBottom="48dp"
        android:text="第二种"
        app:layout_constraintBottom_toTopOf="@+id/btn_3"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btn_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三种"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/btn_2"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="48dp"
        android:text="第四种"
        app:layout_constraintStart_toStartOf="@+id/btn_3"
        app:layout_constraintTop_toBottomOf="@+id/btn_3" />
</androidx.constraintlayout.widget.ConstraintLayout>

3:后端完整代码

package cn.cg.lickeventsummary;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements  View.OnClickListener {

    private Button  btn_2,btn_3,btn_4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_2= findViewById(R.id.btn_2);
        btn_3=findViewById(R.id.btn_3);
        btn_4=findViewById(R.id.btn_4);
        //对btn_2进行监听
        btn_2.setOnClickListener(new View.OnClickListener() {
            //【2】使用匿名内部类实现点击事件
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"使用匿名内部类实现点击事件",Toast.LENGTH_LONG).show();
            }
        });
        //【3】使用内部类实现点击事件
        btn_3.setOnClickListener(new btn3_onClickListener());

       //【4】让MainActivity实现View.OnClickListener接口
        btn_4.setOnClickListener(this);
    }
    
    //【1】按钮1点击事件方法
    public void btn_onclick1(View view) {
        Toast.makeText(MainActivity.this,"通过布局属性实现点击功能",Toast.LENGTH_LONG).show();
    }

    //【4】让MainActivity重写onClick方法
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this,"让MainActivity实现View.OnClickListener接口",Toast.LENGTH_LONG).show();
    }


    //【3】内部类实现点击事件
    private class btn3_onClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,"使用内部类实现点击事件",Toast.LENGTH_LONG).show();
        }
    }
}

六:运行效果

1:点击第一种
在这里插入图片描述
2:点击第二种
在这里插入图片描述
3:点击第三种
在这里插入图片描述
4:点击第四种
在这里插入图片描述

发布了63 篇原创文章 · 获赞 23 · 访问量 6662

猜你喜欢

转载自blog.csdn.net/weixin_42753193/article/details/105404289