前言
安卓自带RadioGroup方向水平
效果
代码
ZnRadioGroup
package widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
/**
* Created on 2018/3/31.
*
* @desc RadioGroup
*/
public class ZnRadioGroup extends RadioGroup {
private OnCheckedChangeListener mOnCheckedChangeListener;
public ZnRadioGroup(Context context) {
super(context);
}
public ZnRadioGroup(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(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
button.setChecked(true);
checkRadioButton(button);
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(ZnRadioGroup.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
} 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
} else {
button.setChecked(false);
}
}
}
}
}
}
}
布局
<widget.ZnRadioGroup
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.ZnRadioGroup>
主代码
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;
}
});