Android学习笔记之View点击事件的四种写法

本文深入讲解了Android中View的四种点击事件实现方法,包括使用onClick属性、内部类、匿名内部类及主类实现接口的方式,提供了详细的代码示例。

View的四种点击事件的写法

参考《第一行代码 第二版》《Android编程权威指南 第二版》

关于View点击事件

所谓View我所理解就是所有控件的祖宗,即他也代表所有Android控件的集合。

实现方法一:

适合只有一个按使用;

使用onClick属性,通过在xml预览界面对Button控件的onClick属性进行赋值;从而实现View点击事件

实现方法二

适合多个按钮使用,可配合Switch循环食用更加美味;

使用内部类实现OnClickListener接口,并实现接口的Onclick()方法,完成的View点击事件

实现方法三

适用与按钮较少或只有一个按钮时;

使用匿名内部类,实现OnClickListener接口,并实现onClick()方法;

实现方法四

适用于多个按钮可配合Switch循环食用更加美味;

使用主类类实现OnClickListener接口,并实现接口的Onclick()方法,完成的View点击事件

PS在这里说一个重要的方法:setOnClickListener()时一个监听器函数,使用的时候要注意

下面我分别放上xml界面的代码和java实现代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android四种点击事件的写法"
        android:textSize="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <!--使用onClick属性-->
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="46dp"
        android:onClick="click"
        android:text="使用onClick属性实现点击事件"
        android:textSize="15dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <!--使用内部类实现点击事件-->
    <Button
        android:id="@+id/bu_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="使用内部类实现点击事件"
        android:textSize="15dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2" />
    <!--使用匿名内部类实现-->
    <Button
        android:id="@+id/Bu_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"
        android:text="使用匿名内部类实现点击事件"
        android:textSize="15dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bu_2" />
    <!--使用主类实现接口实现点击事件-->
    <Button
        android:id="@+id/bu_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="168dp"
        android:text="使用主类实现接口实现点击事件"
        android:textSize="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

java代码

package com.example.jackson.button_four;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
//主类实现OnClickListener接口
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this,"主类实现接口",Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button) findViewById(R.id.Bu_3);//匿名匿名内部类
        Button button1=(Button) findViewById(R.id.bu_2);//内部类按钮
        button1.setOnClickListener(new A());//内部类监听器
        Button button2=(Button) findViewById(R.id.bu_4);//主类实现按钮
        button2.setOnClickListener(this);//主类实现监听器

        //匿名内部类
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"匿名内部类",Toast.LENGTH_SHORT).show();
            }
        });
    }

    //使用onClick属性完成点击事件
    public void click(View v)
    {
        Toast.makeText(MainActivity.this,"onclick",Toast.LENGTH_SHORT).show();
    }
    private class A implements View.OnClickListener//内部类实现接口
    {
        @Override
        public void onClick(View v) {
            if (v.getId()==R.id.bu_2)
            {
                Toast.makeText(MainActivity.this,"内部类",Toast.LENGTH_SHORT).show();
            }
        }
    }



}

搞定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值