android 中关于多行RadioGroup只选择一个RadioButton的解决方法

在Android开发中,遇到多行显示的RadioGroup,要求只能选择一个RadioButton。尝试将RadioButton放入LinearLayout中设置orientation,但未能实现预期效果。最终通过自定义组件成功解决问题,适合新手参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天博主在项目中遇到了一个问题,有多行数据用RadioGroup来显示,但是,只能选中这个多行RadioGroup中的一个radiobutton,刚开始我想的是用一个radiogroup做最外层,然后里面用LinearLayout来包裹这些radiobutton,以便于进行orientation属性的设置,但是,这样设置完成后,却不能实现博主的功能,无奈,只得自定义。代码如下:

public class RadioGroupUtils extends RadioGroup {
    private OnCheckedChangeListener onCheckedChangeListener;

    public RadioGroupUtils(Context context) {
        super(context);
    }

    public RadioGroupUtils(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        onCheckedChangeListener = listener;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void addView(final View child, int index, ViewGroup.LayoutParams params) {
        if (child instanceof ViewGroup) {
            int childCount = ((ViewGroup) child).getChildCount();
   
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值