Android CheckBox と CompoundButton のソースコード解析

チェックボックスとCompoundButtonはどちらも Android の View コントロールです。どちらも Button クラスを継承するため、Button の一部のプロパティとメソッドを備えています。以下で、CheckBox と CompoundButton のソースコードについてそれぞれ説明します。

  1. CheckBoxのネイティブソース分析:

チェックボックスは、選択状態か非選択状態かを示すことができるオプションの旗です。チェックボックスはCompoundButtonクラスを継承しています。以下はチェックボックスの一部ソースコードの解析です:

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

チェックボックスのコンストラクタはCompoundButton親クラスのコンストラクタをパラメータを付与した上で呼び出している。CompoundButtonはButtonクラスを継承し、Checkableインタフェースを実装した抽象クラスで、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では選択状態の設定・取得,および選択状態変化リスナーの追加を行うメソッドやプロパティが定義されます。 コンストラクタでは引数で指定された値から選択状態が初期化されます。 setChecked()メソッドは選択状態を設定し,状態変化時にリスナーに通知します。 isChecked()メソッドは現在の選択状態を取得します。

要約:

CheckBoxクラスはチェック状態を制御するCompoundButtonのサブクラスであり、CompoundButtonはButtonのサブクラスで、Checkableインターフェースを実装しています。CompoundButtonは、チェック状態に関連するメソッドとプロパティ、そしてチェック状態が変化したときのリスナーインターフェースが定義されています。

bannerAds