Android 再次选中收音机组中的单选按钮
我正在使用一个包含两个单选按钮的单选组,如下所示:Android 再次选中收音机组中的单选按钮,android,kotlin,radio-button,radio-group,Android,Kotlin,Radio Button,Radio Group,我正在使用一个包含两个单选按钮的单选组,如下所示: <RadioGroup android:layout_width="match_parent" android:layout_marginStart="@dimen/dp_16" android:layout_marginEnd="@dimen/dp_16" android:l
<RadioGroup
android:layout_width="match_parent"
android:layout_marginStart="@dimen/dp_16"
android:layout_marginEnd="@dimen/dp_16"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rbDebitCard"
android:fontFamily="@font/sf_ui_regular"
android:textColor="@color/txt_color_heading"
android:layout_width="match_parent"
android:drawableEnd="@drawable/debit_card"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pay with Card"/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_1"
android:layout_marginTop="@dimen/dp_16"
android:background="@color/divider_line"/>
<RadioButton
android:id="@+id/rbCash"
android:fontFamily="@font/sf_ui_regular"
android:textColor="@color/txt_color_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:drawableEnd="@drawable/cash"
android:layout_marginTop="@dimen/dp_16"
android:text="Cash"/>
</RadioGroup>
rgPayment.setOnCheckedChangeListener { group, checkedId ->
when(checkedId){
R.id.rbDebitCard->{
rbDebitCard.isChecked = true
paymentMode = "Card"
onBraintreeSubmit(clContainer)
}
R.id.rbCash->{
paymentMode = "Cash"
}
}
}
将检查更改侦听器添加到单选按钮如下所示:
<RadioGroup
android:layout_width="match_parent"
android:layout_marginStart="@dimen/dp_16"
android:layout_marginEnd="@dimen/dp_16"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rbDebitCard"
android:fontFamily="@font/sf_ui_regular"
android:textColor="@color/txt_color_heading"
android:layout_width="match_parent"
android:drawableEnd="@drawable/debit_card"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pay with Card"/>
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_1"
android:layout_marginTop="@dimen/dp_16"
android:background="@color/divider_line"/>
<RadioButton
android:id="@+id/rbCash"
android:fontFamily="@font/sf_ui_regular"
android:textColor="@color/txt_color_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="true"
android:drawableEnd="@drawable/cash"
android:layout_marginTop="@dimen/dp_16"
android:text="Cash"/>
</RadioGroup>
rgPayment.setOnCheckedChangeListener { group, checkedId ->
when(checkedId){
R.id.rbDebitCard->{
rbDebitCard.isChecked = true
paymentMode = "Card"
onBraintreeSubmit(clContainer)
}
R.id.rbCash->{
paymentMode = "Cash"
}
}
}
您必须获取无线电组的id并使用无线电组checkedchangedlistener,如下所示-
val checkedRadioButton = group?.findViewById(group.checkedRadioButtonId) as? RadioButton
checkedRadioButton?.let {
if (checkedRadioButton.isChecked)
Toast.makeText(applicationContext, "RadioGroup: ${group?.contentDescription} RadioButton: ${checkedRadioButton?.text}", Toast.LENGTH_LONG).show()
}
您必须获取无线电组的id并使用无线电组checkedchangedlistener,如下所示-
val checkedRadioButton = group?.findViewById(group.checkedRadioButtonId) as? RadioButton
checkedRadioButton?.let {
if (checkedRadioButton.isChecked)
Toast.makeText(applicationContext, "RadioGroup: ${group?.contentDescription} RadioButton: ${checkedRadioButton?.text}", Toast.LENGTH_LONG).show()
}
问题在于
rbDebitCard.isChecked=false
我将其替换为rgPayment.clearCheck()
因此,我认为在放射组中,一些检查必须由单选组进行维护,这与手动分配给单选按钮的检查无关
但是,这件事解决了问题:)问题在于
rbDebitCard.isChecked=false
我将其替换为rgPayment.clearCheck()
因此,我认为在放射组中,一些检查必须由单选组进行维护,这与手动分配给单选按钮的检查无关
然而,这件事解决了这个问题:)不。它一次又一次地检查单选按钮。因此,底部板材也会再次打开检查选中了哪个单选按钮。你可以设置一个if条件。基于此,呼叫您的BootomSheetI也像您一样为braintree付款做了同样的事情。这很有效。您可能缺少一些内容。我已使用检查更改侦听器代码更新了问题。你能看一下吗。它一次又一次地检查单选按钮。因此,底部板材也会再次打开检查选中了哪个单选按钮。你可以设置一个if条件。基于此,呼叫您的BootomSheetI也像您一样为braintree付款做了同样的事情。这很有效。您可能缺少一些内容。我已使用检查更改侦听器代码更新了问题。你能看一下吗。