自訂CheckBox樣式,checkbox樣式
1.自訂
2.準備圖片選中和未選中的圖片
2.1 圖片和檔案名稱
btn_check_off.png btn_check_on.png
2.2 項目結構
3.編寫樣式檔案
3.1 專案檔結構
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() {
public void onCheckedChanged(CompoundButtonbuttonView,booleanisChecked) {
Toast.makeText(CheckBoxActivity.this , "選中狀態:" + (isChecked ?"選中" :"未選中"), Toast.LENGTH_SHORT).show();
}
});