我想从四个复选框中选择一个复选框。 并将值保存在变量中。所有四个复选框都包含水平对齐的TextView。
答案 0 :(得分:0)
您可以像这样使用广播组
您活动的java代码
final RadioGroup radioGroup = (RadioGroup) cancelDialog.findViewById(R.id.cancle_booking_radio_group);
radioChangePlan = (RadioButton) cancelDialog.findViewById(R.id.radioChangePlan);
radioBetterDeal = (RadioButton) cancelDialog.findViewById(R.id.radioBetterDeal);
radioDiffHotel = (RadioButton) cancelDialog.findViewById(R.id.radioDiffHotel);
radioBookingMistake = (RadioButton) cancelDialog.findViewById(R.id.radioBookingMistake);
radioOther = (RadioButton) cancelDialog.findViewById(R.id.radioOther);
tvHideCancelDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cancelDialog.dismiss();
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
View radioButton = radioGroup.findViewById(checkedId);
int index = radioGroup.indexOfChild(radioButton);
switch (index) {
case 0:
// first radio button selected
break;
case 1:
// second radio button selected
break;
case 2:
// third radio button selected
break;
case 3:
// forth radio button selected
break;
case 4:
// fifth radio button selected
break;
}
}
});
xml布局
<RadioGroup
android:id="@+id/cancle_booking_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<RadioButton
android:id="@+id/radioChangePlan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimary"
android:text="@string/change_in_plan" />
<RadioButton
android:id="@+id/radioBetterDeal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimary"
android:text="@string/found_a_better_deal" />
<RadioButton
android:id="@+id/radioDiffHotel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimary"
android:text="@string/want_to_book_a_differnt_hotel" />
<RadioButton
android:id="@+id/radioBookingMistake"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimary"
android:text="@string/booking_created_by_mistake" />
<RadioButton
android:id="@+id/radioOther"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:buttonTint="@color/colorPrimary"
android:text="@string/other" />
</RadioGroup>