Android中UI控件button的四种点击方式

本文介绍了Android中button控件的四种点击事件实现方式:匿名内部类、内部类、实现View.OnClickListener接口以及使用android:onClick属性。详细讲解了每种方式的步骤、优缺点,并强调在实际开发中,为遵循MVC模式,通常推荐使用内部类实现点击事件。
摘要由CSDN通过智能技术生成

一、常用控件button简介
button属于android.Widget包,并且继承了android.Widget.TextView类,提供了按钮控制的功能。
二、四种点击方式简介
1、使用匿名内部类创建点击事件
2、使用内部类创建点击事件
3、使用View.OnClickListener接口
4、使用button的xml属性 android:onClick
三、四种点击方式详细介绍
1、使用匿名内部类创建点击事件
这是十分常用的方式,并且也是步骤最简单的方式。
先在xml文件中直接创建4个按钮控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/main_button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="按钮1"
        />
    <Button
        android:id="@+id/main_button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="按钮2"
        />
    <Button
        android:id="@+id/main_button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="按钮3"
        />
    <Button
        android:id="@+id/main_button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="按钮4"
        />

</LinearLayou
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值