gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:30:06 25 4
gpt4 key购买 nike

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

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

最佳答案

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

关于android - 在单选组 android 中再次检查单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56847074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com