第一:CheckBox 要设置背景,不设置单击前后背景,会显示手机默认的背景,可能会不好看,
第一步:drawable文件夹下新建方法 checkbox 设置单击前后背景变换;@drawable/checkbox_pressed2x,单击后图片背景;@drawable/checkbox2x,单击前背景图片
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="@drawable/checkbox_pressed2x"/>
<item android:state_checked="false" android:drawable="@drawable/checkbox2x"/>
<item android:drawable="@drawable/checkbox2x"></item>
</selector>
第二步:styles 中添加方法 CheckBoxStyle 引用 drawadle 中设置的xml文件 用<item name="android:button">@drawable/checkbox</item> 方法引用;parent="@android:style/Widget.CompoundButton.CheckBox">,调用系统的参数,设定这个方法给单选框checkbox用的
<style name="CheckBoxStyle" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/checkbox</item>
</style>
第三步:组件中调用 styles中 方法
style="@style/CheckBoxStyle"
第二:多个checkbox不使用单选按钮组的时候如何判断被选中(建议使用单选按钮组来做单选的功能)
if(yigeyue.isChecked()){//被选中
Toast.makeText(this, "一个月", Toast.LENGTH_LONG).show();
}
yigeyue.setChecked(false);//设置不被选中
yigeyue.setClickable(false);//设置不可以被单击