117.info
人生若只如初见

Android中CheckBox与CompoundButton源码解析

CheckBox和CompoundButton都是Android中的View控件,它们都继承自Button类,因此它们具有Button的一些属性和方法。下面分别对CheckBox和CompoundButton的源码进行解析。

  1. CheckBox源码解析:

CheckBox是一个可选的标志,可以用于表示选中或未选中状态。CheckBox继承自CompoundButton类。以下是CheckBox的部分源码解析:

public class CheckBox extends CompoundButton {
// 构造方法
public CheckBox(Context context) {
this(context, null);
}
public CheckBox(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.checkboxStyle);
}
public CheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public CheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
}

在构造方法中,CheckBox调用了父类CompoundButton的构造方法,传递了相应的参数。CompoundButton是一个抽象类,它继承自Button类,并实现了Checkable接口。CompoundButton中定义了一些与选择状态相关的方法和属性,例如setChecked()、isChecked()等。

  1. CompoundButton源码解析:

CompoundButton是一个抽象类,它继承自Button类,并实现了Checkable接口。以下是CompoundButton的部分源码解析:

public abstract class CompoundButton extends Button implements Checkable {
// 选中状态改变监听器
private OnCheckedChangeListener mOnCheckedChangeListener;
// 按钮的选中状态
private boolean mChecked;
// 是否正在设置选中状态
private boolean mBroadcasting;
// 构造方法
public CompoundButton(Context context) {
this(context, null);
}
public CompoundButton(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.buttonStyle);
}
public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
// 初始化选中状态
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.CompoundButton, defStyleAttr, defStyleRes);
boolean checked = a.getBoolean(R.styleable.CompoundButton_android_checked, false);
setChecked(checked);
a.recycle();
}
}
// 设置选中状态
public void setChecked(boolean checked) {
if (mChecked != checked) {
mChecked = checked;
refreshDrawableState();
// 通知选中状态改变
if (!mBroadcasting) {
mBroadcasting = true;
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
}
mBroadcasting = false;
}
}
}
// 获取选中状态
public boolean isChecked() {
return mChecked;
}
// 添加选中状态改变监听器
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
// 选中状态改变监听器接口
public interface OnCheckedChangeListener {
void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
}
}

CompoundButton中定义了一些方法和属性,用于设置和获取选中状态,以及添加选中状态改变监听器。在构造方法中,CompoundButton会根据传入的属性初始化选中状态。setChecked()方法用于设置选中状态,并在状态改变时通知监听器。isChecked()方法用于获取当前的选中状态。

总结:

CheckBox是一个可选的标志,继承自CompoundButton类,而CompoundButton是一个抽象类,继承自Button类,并实现了Checkable接口。CompoundButton中定义了一些与选择状态相关的方法和属性,以及选中状态改变的监听器接口。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe384AzsLBA5eDFM.html

推荐文章

  • android lifecycle怎么使用

    Android生命周期是指一个Android应用程序的运行状态,包括应用程序创建、启动、暂停、恢复、停止和销毁等阶段。
    使用Android生命周期可以控制应用程序在不同...

  • android lifecycle原理是什么

    Android生命周期是指一个Android应用在其整个运行过程中所经历的不同阶段。它是由Android系统控制和管理的,开发者可以通过重写指定的方法来在不同的阶段执行自己...

  • android recyclerview缓存机制是什么

    Android RecyclerView的缓存机制是通过RecyclerView的LayoutManager进行管理的。LayoutManager负责测量和布局RecyclerView中的子项,并在需要显示时重用已经布局...

  • android thread怎么使用

    在Android中,可以使用Thread类来创建线程。以下是使用Android Thread的基本步骤: 创建一个类来继承Thread类,并重写run()方法,在run()方法中定义线程要执行的...

  • J2EE是什么意思2又是什么意思

    J2EE是Java 2 Platform, Enterprise Edition的缩写,意思是Java 2企业级平台。它是一种用于开发企业级应用程序的Java平台,提供了一套标准的API和工具,用于构建...

  • 什么是Peoplesoft

    PeopleSoft是一种商业管理软件,用于企业资源规划(ERP)和人力资源管理(HRM)。它于1987年由德文默里(Dave Duffield)和肯莫里森(Ken Morris)创立,并于198...

  • linux怎样安装rpm文件

    要在Linux系统上安装.rpm文件,您可以使用以下步骤: 打开终端,并切换到具有管理员权限的用户。 使用以下命令安装rpm软件包: sudo rpm -i package_name.rpm 或...

  • 我来给你讲:VBA究竟是什么

    VBA(Visual Basic for Applications)是一种编程语言,用于在Microsoft Office套件中自动化和定制化任务。它是Visual Basic的一个变种,专门用于编写宏(或称为...