测试checkbox的activity
public class Ch4_CheckBoxActivity extends AppCompatActivity
implements CompoundButton.OnCheckedChangeListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox_ch4);
CheckBox ck_system = findViewById(R.id.ch4_ck_system);
CheckBox ck_custom = findViewById(R.id.ch4_ck_custom);
ck_system.setOnCheckedChangeListener(this);
ck_custom.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String desc = String.format("你%s了这个Checkbox", isChecked ? "勾选" : "取消勾选");
buttonView.setText(desc);
}
private class CheckListener implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String desc = String.format("您勾选了控件%d,状态为%b", buttonView.getId(), isChecked);
Toast.makeText(Ch4_CheckBoxActivity.this, desc, Toast.LENGTH_LONG).show();
}
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<CheckBox
android:id="@+id/ch4_ck_system"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
android:padding="5dp"
android:text="这是系统的checkbox"
android:textColor="@color/black"
android:textSize="17sp" />
<CheckBox
android:id="@+id/ch4_ck_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@mipmap/ic_launcher_round"
android:checked="false"
android:padding="5dp"
android:text="这个checkbox换了图标"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
点击checkbox