首页 > 学院 > 开发设计 > 正文

自定义CheckBox样式

2019-11-14 23:04:03
字体:
来源:转载
供稿:网友
自定义CheckBox样式

1.自定义效果图

clip_image001[11]clip_image002[11]

2.准备图片选中和未选中的图片

2.1 图片和文件名

clip_image003[11] clip_image004[11]

btn_check_off.png btn_check_on.png

2.2 项目结构

clip_image005[11]

3.编写样式文件

3.1 项目文件结构

clip_image006[11]

  3.2 checkbox_style.xml代码

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

<itemandroid:drawable="@drawable/btn_check_on"android:state_checked="true"/>

<itemandroid:drawable="@drawable/btn_check_off"android:state_checked="false"/>

</selector>

4.编写页面文件main.xml

<CheckBox

android:id="@+id/checkBox1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:button="@drawable/checkbox_style"

android:text="CheckBox"/>

5.状态改变事件

CheckBox chk = (CheckBox) findViewById(R.id.checkBox1);

chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

publicvoidonCheckedChanged(CompoundButton buttonView, booleanisChecked) {

Toast.makeText(CheckBoxActivity.this , "选中状态:" + (isChecked ? "选中" : "未选中"), Toast.LENGTH_SHORT).show();

}

});


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表