实现结果:
1. 要求:
- 两行两列显示
- 仅支持单选;
2. 自定义多选 MultiLineRadioGroup.java (部分代码)
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);
}
}
}
}
3. 页面布局 .xml
<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>
4. 数据回填选中按钮变色
/**
* 每行显示的个数
*/
public static final int MAX_COUNT = 2;
/**
* 回填选中button
* @param radioGroup
* @param pos
*/
private void radioButtonPerformClick(MultiLineRadioGroup radioGroup, int pos) {
radioGroup.clearAllButtonChecked();
int group = pos / MAX_COUNT;//显示在第几行
int index = pos % MAX_COUNT;//单行的位置
View child = radioGroup.getChildAt(group);
View view = ((LinearLayout) child).getChildAt(index);
if (view instanceof RadioButton) {
final RadioButton rb = (RadioButton) view;
rb.performClick();
}
}
ending….
版权声明:本文为jslhl原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。