在RadioButton上使用OnCheckedChangeListener反向显示/隐藏布局 [英] Reverse show / hide of layouts with OnCheckedChangeListener on a RadioButton
问题描述
代码
对于我的应用程序中的结帐,我让用户通过在RadioButton组中选择付款方式来决定他要使用的付款方式:
For the checkout in my app, I let the user decide the payment method he wants to use by selecting it in a RadioButton Group:
<RadioGroup
android:id="@+id/payment_method"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/radio_prepaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="prepaid"
android:tag="prepaid"
/>
<RadioButton
android:id="@+id/radio_creditcard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="creditcard"
android:textAllCaps="true"
android:tag="creditcard"
/>
</RadioGroup>
从用户选择中,我要显示或隐藏匹配的布局:
From the user selection I want to show or hide the matching Layout:
<RelativeLayout
android:id="@+id/relativeLayout_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
>
...
</RelativeLayout>
或
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cL_preapid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
>
...
</ConstraintLayout>
我是用这部分代码做到的:
I did it with this part of code:
CompoundButton.OnCheckedChangeListener onPaymentChangedListener = (compoundButton,b) -> {
if(compoundButton.getTag().equals("prepaid")) {
relativeLayout_card.setVisibility(View.GONE);
cl_prepaid.setVisibility(View.VISIBLE);
}
if(compoundButton.getTag().equals("creditcard")) {
cl_prepaid.setVisibility(View.GONE);
relativeLayout_card.setVisibility(View.VISIBLE);
}
};
rb_creditcard.setOnCheckedChangeListener(onPaymentChangedListener);
rb_prepaid.setOnCheckedChangeListener(onPaymentChangedListener);
问题
1.单击在我的预付费/信用卡"单选按钮上,可以完美显示匹配的视图
1. CLICK on my Prepaid / CreditCard RadioButton it shows the matching view perfectly
2.在我的Prepaid/CreditCard单选按钮上单击,它根本不执行任何操作
2. CLICK on my Prepaid / CreditCard RadioButton it does simply nothing
3.单击,在我的Prepaid/CreditCard单选按钮上,它会颠倒视图:"prepaid"显示CreditCard布局,"creditcard"显示我PrepaidPay Layout
3. CLICK on my Prepaid / CreditCard RadioButton it reverses the views: "prepaid" shows the CreditCard Layout, "creditcard" shows me PrepaidPay Layout
这是布局问题吗(我应该实现ViewSwwichter/ViewPager)还是我的代码有错误?
Is this a problem with the Layouts (should I implement a ViewSwwichter / ViewPager) or is there an error in my code?
谢谢
推荐答案
而不是每个 RadioButton
尝试在 RadioGroup
payment_method.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
if(checkedRadioButton.isChecked()) {
if (checkedId == R.id.radio_prepaid)
{
relativeLayout_card.setVisibility(View.GONE);
cl_prepaid.setVisibility(View.VISIBLE);
}
else if (checkedId == R.id.radio_creditcard)
{
cl_prepaid.setVisibility(View.GONE);
relativeLayout_card.setVisibility(View.VISIBLE);
}
}
}
});
这篇关于在RadioButton上使用OnCheckedChangeListener反向显示/隐藏布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!