0
我想从四个复选框中选择一个复选框。 并将值保存在一个变量中。所有四个复选框都包含水平对齐的TextView。如何在运行时在android中仅选择一个复选框
我想从四个复选框中选择一个复选框。 并将值保存在一个变量中。所有四个复选框都包含水平对齐的TextView。如何在运行时在android中仅选择一个复选框
可以用户单选组像你的活动这
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>
你为什么要使用复选框此功能使用单选按钮 – sumit
你可以使用收音机组 –
欢迎使用堆栈溢出。我们在这里帮助你的代码。请提供你所尝试的代码,以便有人可以帮助你。 – Syfer