简介
安卓自带RadioGroup方向水平。
多行
效果
代码
HorizontalVerticalRadioGroup
package widget.button;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import util.LogUtils;
/**
* Created on 2019/5/23.
*
* @author 郑少鹏
* @desc 水平垂直RadioGroup
*/
public class HorizontalVerticalRadioGroup extends RadioGroup {
private OnCheckedChangeListener mOnCheckedChangeListener;
public HorizontalVerticalRadioGroup(Context context) {
super(context);
}
public HorizontalVerticalRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
@Override
public void addView(final 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(HorizontalVerticalRadioGroup.this, button.getId());
}
return true;
});
}
}
}
super.addView(child, index, params);
}
private void checkRadioButton(RadioButton radioButton) {
View child;
int radioCount = getChildCount();
for (int i = 0; i < radioCount; i++) {
child = getChildAt(i);
if (child instanceof RadioButton) {
if (child == radioButton) {
// do nothing
LogUtils.e("child == radioButton");
} else {
((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 button = (RadioButton) view;
if (button == radioButton) {
// do nothing
LogUtils.e("child == radioButton");
} else {
button.setChecked(false);
}
}
}
}
}
}
}
布局
<widget.button.HorizontalVerticalRadioGroup
android:id="@+id/rgTimeCoin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/d12"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbTimeCoin1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/blue_rabutton_selector"
android:button="@null"
android:checked="true"
android:gravity="center"
android:paddingBottom="@dimen/d6"
android:paddingLeft="@dimen/d12"
android:paddingRight="@dimen/d12"
android:paddingTop="@dimen/d6"
android:text="100"
android:textColor="@drawable/tv_blue_white_en_selector"
android:textSize="@dimen/s15" />
...
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/d12"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbTimeCoin1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/blue_rabutton_selector"
android:button="@null"
android:checked="true"
android:gravity="center"
android:paddingBottom="@dimen/d6"
android:paddingLeft="@dimen/d12"
android:paddingRight="@dimen/d12"
android:paddingTop="@dimen/d6"
android:text="100"
android:textColor="@drawable/tv_blue_white_en_selector"
android:textSize="@dimen/s15" />
...
</LinearLayout>
</widget.button.HorizontalVerticalRadioGroup>
主代码
rgTimeCoin.setOnCheckedChangeListener((group, checkedId) -> {
switch (checkedId) {
case R.id.rbTimeCoin1:
timeCoin = rbTimeCoin1.getText().toString();
break;
case R.id.rbTimeCoin2:
timeCoin = rbTimeCoin2.getText().toString();
break;
case R.id.rbTimeCoin3:
timeCoin = rbTimeCoin3.getText().toString();
break;
case R.id.rbTimeCoin4:
timeCoin = rbTimeCoin4.getText().toString();
break;
case R.id.rbTimeCoin11:
timeCoin = rbTimeCoin11.getText().toString();
break;
case R.id.rbTimeCoin12:
timeCoin = rbTimeCoin12.getText().toString();
break;
case R.id.rbTimeCoin13:
timeCoin = rbTimeCoin13.getText().toString();
break;
case R.id.rbTimeCoin14:
timeCoin = rbTimeCoin14.getText().toString();
break;
default:
break;
}
});
不足
不可实现取选。
取选
单行
radioGroup.clearCheck();
多行
日志
StackOveflowError
分析
RadioGroup多行显即多RadioGroup切换,通RadioGroup之clearCheck()操作。clearCheck()前RadioGroup设监听则执行报StackOveflowError错。
解决
radioGroupOne.setOnCheckedChangeListener(new OneCheckedChangeListener());
radioGroupTwo.setOnCheckedChangeListener(new TwoCheckedChangeListener());\
/**
* 一筛选
*/
class OneCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
/**
* <p>Called when the checked radio button has changed. When the
* selection is cleared, checkedId is -1.</p>
*
* @param group the group in which the checked radio button has changed
* @param checkedId the unique identifier of the newly checked radio button
*/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
radioGroupTwo.setOnCheckedChangeListener(null);
radioGroupTwo.clearCheck();
radioGroupTwo.setOnCheckedChangeListener(new TwoCheckedChangeListener());
switch (checkedId) {
// 婴儿(0-1)
case R.id.bottomSheetDialogMemberScreenMrbBaby:
break;
....
default:
break;
}
}
}
/**
* 二筛选
*/
class TwoCheckedChangeListener implements RadioGroup.OnCheckedChangeListener {
/**
* <p>Called when the checked radio button has changed. When the
* selection is cleared, checkedId is -1.</p>
*
* @param group the group in which the checked radio button has changed
* @param checkedId the unique identifier of the newly checked radio button
*/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
radioGroupOne.setOnCheckedChangeListener(null);
radioGroupOne.clearCheck();
radioGroupOne.setOnCheckedChangeListener(new OneCheckedChangeListener());
switch (checkedId) {
// 少年(12-18)
case R.id.bottomSheetDialogMemberScreenMrbEarlyYouth:
break;
....
default:
break;
}
}
}