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

时间:2019-07-02 07:08:04

标签: 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: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时,我将显示一个底部表单,并在关闭底部表单时通过ischecked = false更改检查。 可以完全取消选中单选按钮。

问题是,当我再次单击同一单选按钮时,没有得到检查。但是,底页正在打开。

如果我单击另一个单选按钮(显然已选中),然后再次单击第一个按钮,则它将被选中并打开底页。但是,我再次关闭了底页,然后单击单选按钮,它没有被检查。

作为参考,下面是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"
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

您必须获取Radio Group的ID并像这样使用Radio Group 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()
}

答案 1 :(得分:1)

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

为此,我认为我的无线电组必须由无线电组维护一些检查,这与手动分配给单选按钮的检查无关。

但是,这件事解决了这个问题:)