代码如下:
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>
网友评论