1.自定義效果圖
![clip_image001[11] clip_image001[11]](http://s1.VeVb.com/20150728/ap0digkjao559.png)
![]()
2.準(zhǔn)備圖片選中和未選中的圖片
2.1 圖片和文件名
![clip_image004[11] clip_image004[11]](http://s1.VeVb.com/20150728/h4lub03hjiz59.png)
btn_check_off.png btn_check_on.png
2.2 項(xiàng)目結(jié)構(gòu)
![clip_image005[11] clip_image005[11]](http://s1.VeVb.com/20150728/43r5uwmxi3m59.png)
3.編寫樣式文件
3.1 項(xiàng)目文件結(jié)構(gòu)
![]()
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.狀態(tài)改變事件
CheckBox chk = (CheckBox) findViewById(R.id.checkBox1);
chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
publicvoidonCheckedChanged(CompoundButton buttonView, booleanisChecked) {
Toast.makeText(CheckBoxActivity.this , "選中狀態(tài):" + (isChecked ? "選中" : "未選中"), Toast.LENGTH_SHORT).show();
}
});
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注