常用属性
id 用于识别控件的唯一标识
layout_width 组件宽度
layout_height 组件高度
text 复选框后的文字
textSize 文字大小
textColor 文字颜色
layout_below
自定义样式
bg_checkbox.xml(自定义样式文件)
<selector xmlns:...>
<!--复选框未被选择时的样式设置-->
<item android:state_checked="false"
andrroid:drawable="@drawable/icon_checkbox_false"/>
<!--复选框被选择时的样式设置-->
<item android:state_checked="true"
android:drawable="@drawable/icon_checkbox_true"/>
</selector>
复选框样式文件,使用复选框自定义样式的相关代码
<CheckBox
...
android:button="@drawable/bg_checkbox"
android:paddingLeft="10dp"
.../>
其中:
button=“@drawable/bg_checkbox” 表示复选框选项按钮使用自定义的复选框样式
paddingLeft=“10dp’” 表示复选框与右侧文字之间的距离
监听事件
//声明控件
private CheckBox mCb5,mCb6;
//找到控件
mCb5 = (CheckBox) findViewById(R.id.cb_5);
mCb6 = (CheckBox) findViewById(R.id.cb_6);
//设置监听事件
mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
Toast.makeText(CheckBoxActivity.this, isChecked ? "5被选中" : "5未被选中", Toast.LENGTH_SHORT).show();
}
});
mCb6.setOnCheckedChangeLostener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
Toast.makeText(CheckBoxActivity.this, isChecked ? "6被选中" : "6未被选中" , Toast.LENGTH_SHORT).show();
}
});