简体   繁体   English

在电台组android中再次检查单选按钮

[英]check radio button again in radio group android

I am using a radio group containing two radio buttons like below: 我正在使用一个包含两个单选按钮的广播组,如下所示:

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

In my kotlin code I am showing a bottom sheet when rbDebitCard is clicked and changing the check by ischecked=false when closing the bottom sheet. 在我的kotlin代码中,我在单击rbDebitCard时显示底部工作表,并在关闭底部工作表时通过ischecked = false更改检查。 which perfectly unchecks the radio button. 完全取消选中单选按钮。

The issue is, when I am clicking on the same radio button again, it is not getting checked. 问题是,当我再次点击相同的单选按钮时,它没有被检查。 However, the bottom sheet is opening. 但是,底部是打开的。

If i click on the other radio button(which obviously gets checked) and then again clicking on the first button then it is getting checked and opening the bottom sheet. 如果我点击另一个单选按钮(显然会被检查),然后再次单击第一个按钮,则会检查并打开底部工作表。 But once again I close the bottom sheet and click on the radio button, it is not getting checked. 但是我再次关闭底部工作表并单击单选按钮,它没有被检查。

For reference, below is the kotlin code: 供参考,下面是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
        }
    }
}

adding the check change listener to the radio button looks like below: 将检查更改侦听器添加到单选按钮如下所示:

 rgPayment.setOnCheckedChangeListener { group, checkedId ->
        when(checkedId){
            R.id.rbDebitCard->{
                rbDebitCard.isChecked = true
                paymentMode = "Card"
                onBraintreeSubmit(clContainer)
            }

            R.id.rbCash->{
                paymentMode = "Cash"
            }
        }
    }

You have to take the id of the Radio Group and use radio group checkedchangedlistener like this - 您必须使用无线电组的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()
}

the issue is with rbDebitCard.isChecked = false I replaced this with rgPayment.clearCheck() . 问题是rbDebitCard.isChecked = false我用rgPayment.clearCheck()替换了它。

With this i think in a radiogroup some check must be maintained by the radio group also which is independent of the check assigned to a radio button manually. 有了这个,我认为在无线电组中,必须由无线电组维持一些检查,该检查独立于手动分配给单选按钮的检查。

However, this thing solved the issue :) 但是,这件事解决了这个问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM