布局:
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:checked="true"
android:text="男"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="36dp" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
tools:layout_editor_absoluteX="36dp"
tools:layout_editor_absoluteY="114dp" />
實現(xiàn):
final CheckBox box1 = (CheckBox) findViewById(R.id.checkBox1);
final CheckBox box2 = (CheckBox) findViewById(R.id.checkBox2);
box1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
String text = box1.getText().toString();
Log.i("tag", "onCheckedChanged: " + text);
}
});
效果圖: