CheckBox类是继承的CompoundButton,而CompoundButton继承了Button类的同时实现了Checkable接口。因此CheckBox具有Button的一些属性和方法的同时,拥有在Checkable中的方法(例如是否被选中)。
Button类相较于CheckBox,少拥有一些属性和方法。
CheckBox实现点击事件,匿名内部类。
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) Toast.makeText(MainActivity2.this,"你选中了xx选项",Toast.LENGTH_SHORT).show();
}
});
Button实现点击事件,匿名内部类。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});