【Android】RadioGrop 两行两列单选

代码如下:


public class MultiLineRadioGroup extends RadioGroup {
 
    private OnCheckedChangeListener mOnCheckedChangeListener;
 
    public MultiLineRadioGroup(Context context) {
        super(context);
    }
 
    public MultiLineRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
 
    @Override
    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        mOnCheckedChangeListener = onCheckedChangeListener;
    }
 
    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (child instanceof LinearLayout) {
            int childCount = ((LinearLayout) child).getChildCount();
            for (int i = 0; i < childCount; i++) {
                View view = ((LinearLayout) child).getChildAt(i);
                if (view instanceof RadioButton) {
                    final RadioButton button = (RadioButton) view;
                    button.setOnTouchListener((v, event) -> {
                        button.setChecked(true);
                        checkRadioButton(button);
                        if (mOnCheckedChangeListener != null) {
                            mOnCheckedChangeListener.onCheckedChanged(MultiLineRadioGroup.this, button.getId());
                        }
                        return true;
                    });
                }
            }
        }
        super.addView(child, index, params);
    }
 
    private void checkRadioButton(RadioButton button) {
        View child;
        int radioCount = getChildCount();
        for (int i = 0; i < radioCount; i++) {
            child = getChildAt(i);
            if (child instanceof RadioButton && child != button) {
                ((RadioButton) child).setChecked(false);
            } else if (child instanceof LinearLayout) {
                int childCount = ((LinearLayout) child).getChildCount();
                for (int j = 0; j < childCount; j++) {
                    View view = ((LinearLayout) child).getChildAt(j);
                    if (view instanceof RadioButton) {
                        final RadioButton rb = (RadioButton) view;
                        if (rb != button) {
                            ((RadioButton) view).setChecked(false);
                        }
                    }
                }
            }
        }
    }
 
    /**
     * 清除所有的点选效果
     */
    public void clearAllButtonChecked() {
        View child;
        int radioCount = getChildCount();
        for (int i = 0; i < radioCount; i++) {
            child = getChildAt(i);
            int childCount = ((LinearLayout) child).getChildCount();
            for (int j = 0; j < childCount; j++) {
                View view = ((LinearLayout) child).getChildAt(j);
                ((RadioButton) view).setChecked(false);
            }
 
        }
    }
 
 
}





布局如下:


<com.sgs.agent.delivery.widget.dialog.MultiLineRadioGroup
        android:id="@+id/rg_pay_type"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/y39"
        android:orientation="vertical">
 
        <LinearLayout
            android:id="@+id/ll_pay_1"
            android:layout_width="match_parent"
            android:layout_height="@dimen/y128"
            android:orientation="horizontal">
 
            <RadioButton
                android:id="@+id/tab_crash"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/bg_comlib_yellow_transparent_rounded"
                android:button="@null"
                android:checked="true"
                android:gravity="center"
                android:text="@string/comlib_text_delivery_crash_pay"
                android:textColor="@color/text_comlib_checked_black_unchecked_white"
                android:textSize="@dimen/FontSize_48" />
 
            <RadioButton
                android:id="@+id/tab_mail_pay"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/bg_comlib_yellow_transparent_rounded"
                android:button="@null"
                android:gravity="center"
                android:text="@string/comlib_text_delivery_mail_pay"
                android:textColor="@color/text_comlib_checked_black_unchecked_white"
                android:textSize="@dimen/FontSize_48" />
 
        </LinearLayout>
 
        <LinearLayout
            android:id="@+id/ll_pay_2"
            android:layout_width="match_parent"
            android:layout_height="@dimen/y128"
            android:orientation="horizontal">
 
            <RadioButton
                android:id="@+id/tab_month_pay"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/bg_comlib_yellow_transparent_rounded"
                android:button="@null"
                android:gravity="center"
                android:text="@string/sendex_to_cash_monthly"
                android:textColor="@color/text_comlib_checked_black_unchecked_white"
                android:textSize="@dimen/FontSize_48" />
 
            <RadioButton
                android:id="@+id/tab_third_pay"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/bg_comlib_yellow_transparent_rounded"
                android:button="@null"
                android:gravity="center"
                android:text="@string/sendex_to_third_monthly"
                android:textColor="@color/text_comlib_checked_black_unchecked_white"
                android:textSize="@dimen/FontSize_48" />
 
        </LinearLayout>
 
 
    </com.sgs.agent.delivery.widget.dialog.MultiLineRadioGroup>
 




©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容