簡體   English   中英

在電台組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時顯示底部工作表,並在關閉底部工作表時通過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"
            }
        }
    }

您必須使用無線電組的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()替換了它。

有了這個,我認為在無線電組中,必須由無線電組維持一些檢查,該檢查獨立於手動分配給單選按鈕的檢查。

但是,這件事解決了這個問題:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM