Android Studio 获取RadioGroup单选框按钮值

Android Studio 获取RadioGroup单选框按钮值


XML代码

<RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.12"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.323">
        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            />
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            />
    </RadioGroup>

next按钮事件代码

public void next(View view) {
      RadioGroup radioButton =(RadioGroup) findViewById(R.id.radioGroup1);
        String Sex="";//性别
        for(int i=0;i<radioButton.getChildCount();i++){
            RadioButton  r = (RadioButton)radioButton.getChildAt(i);
            if(r.isChecked()){
                Sex = r.getText().toString();
                System.out.println(Sex);
                break;
            }
        }
     }

你可能感兴趣的:(android,studio,android,安卓,app,移动开发)