首页 > 解决方案 > 使用 RadioButton 上的 OnCheckedChangeListener 反向显示/隐藏布局

问题描述

编码

对于我的应用程序中的结帐,我让用户通过在 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>

从用户选择中我想显示或隐藏匹配的布局:

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

我用这部分代码做到了:

    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.点击我的预付/信用卡单选按钮,完美显示匹配视图

2. 点击我的 Prepaid / CreditCard RadioButton 它什么都不做

3. 点击我的 Prepaid / CreditCard RadioButton 它反转视图:“prepaid”显示 CreditCard 布局,“creditcard”显示 PrepaidPay 布局

这是布局的问题(我应该实现 ViewSwwichter / ViewPager)还是我的代码中有错误?

谢谢

标签: javaandroidxmlandroid-layoutandroid-radiobutton

解决方案


而不是每次都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); 

                } 
            }
        }
    });

推荐阅读