安卓b站学习笔记梳理2020.11.26
天哥在奔跑安卓教程b站网址https://www.bilibili.com/video/BV1Rt411e76
目录
8.复选框CheckBox
①常用样式
<CheckBox android:id="@+id/cb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="IOS" android:textSize="20sp" android:layout_below="@id/cb_1" android:layout_marginTop="10dp"/>
②自定义样式
<CheckBox android:id="@+id/cb_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Programming" android:button="@drawable/bg_checkbox" android:paddingLeft="10dp" android:textSize="20sp" android:layout_marginTop="10dp"/>
drawable文件:(前提是导入图片)
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="false" android:drawable="@drawable/check_box_false"/> <item android:state_checked="true" android:drawable="@drawable/check_box_true"/> </selector>
③监听事件
mCb5 = findViewById(R.id.cb_5); mCb6 = findViewById(R.id.cb_6); mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Toast.makeText(CheckBoxActivity.this,isChecked?"5Checked":"5UnChecked",Toast.LENGTH_SHORT).show(); } }); mCb6.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Toast.makeText(CheckBoxActivity.this,isChecked?"6Checked":"6UnChecked",Toast.LENGTH_SHORT).show(); } });