布局:
<RadioGroup
android:id="@+id/groptemp"
android:layout_width="240dp"
android:layout_height="220dp"
android:layout_marginEnd="88dp"
android:layout_marginStart="56dp"
android:layout_marginTop="59dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/radioButton"
android:layout_height="50dp"
android:layout_weight="80"
android:checked="true"
android:text="女" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_height="50dp"
android:layout_weight="80"
android:checked="false"
android:text="男" />
</RadioGroup>
实现:
RadioGroup groupbtn = (RadioGroup) findViewById(R.id.groptemp);
groupbtn.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.radioButton:
Log.i("tag", "onCheckedChanged: 女");
break;
case R.id.radioButton1:
Log.i("tag", "onCheckedChanged: 男");
break;
default:
break;
}
}
});
效果:
Untitled.gif
网友评论