再次检查单选组 android 中的单选按钮

check radio button again in radio group android

我正在使用包含两个单选按钮的单选组,如下所示:

  <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>

在我的 kotlin 代码中,我在单击 rbDebitCard 时显示底部 sheet,并在关闭底部 sheet 时通过 ischecked=false 更改支票。 完全取消选中单选按钮。

问题是,当我再次点击同一个单选按钮时,它没有被选中。不过底部sheet开盘

如果我点击另一个单选按钮(显然被选中)然后再次点击第一个按钮然后它被选中并打开底部 sheet。但是我再次关闭底部 sheet 并单击单选按钮,它没有被选中。

作为参考,下面是 kotlin 代码:

//pay with card
        rbDebitCard.setOnClickListener {
            rbDebitCard.isChecked = true
            paymentMode = "Card"
            onBraintreeSubmit(clContainer)
        }

        //Cash
        rbCash.setOnClickListener {
            paymentMode = "Cash"
        }

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == BundleConstants.REQ_CODE_PAYMENT) {
        if (resultCode == Activity.RESULT_OK) {
            // use the result to update your UI and send the payment method nonce to your server
            val result: DropInResult = data!!.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT)
            Log.e("payment result nonce", result.paymentMethodNonce?.nonce)

            makePayment(result.paymentMethodNonce?.nonce)
        } else if (resultCode == Activity.RESULT_CANCELED) {
            // the user canceled
            rbDebitCard.isChecked = false
        } else {
            // handle errors here, an exception may be available in
            val error = data!!.getSerializableExtra(DropInActivity.EXTRA_ERROR) as Exception
            rbDebitCard.isChecked = false
        }
    }
}

将检查更改侦听器添加到单选按钮如下所示:

 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()
}

问题出在 rbDebitCard.isChecked = false 我将其替换为 rgPayment.clearCheck().

有了这个,我认为在单选组中一些检查必须由单选组维护,这也独立于手动分配给单选按钮的检查。

然而,这个东西解决了问题:)