Android实现RadioGroup的多行多列布局且互斥

1.自定义view
public class MyRadioGroupContainer extends LinearLayout {
private RadioGroup.OnCheckedChangeListener mCheckChangeListener;
private RadioGroup lastRadioGroup;
private RadioGroup.OnCheckedChangeListener changeListener = new RadioGroup.OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton current = (RadioButton) group.findViewById(checkedId);
if (current == null)
return;
if (lastRadioGroup != null && lastRadioGroup != group) {
lastRadioGroup.clearCheck();
}
if (current.isChecked()&&mCheckChangeListener!=null) mCheckChangeListener.onCheckedChanged(group,checkedId);
lastRadioGroup = group;
}
};
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
if (child instanceof RadioGroup) {
((RadioGroup) child).setOnCheckedChangeListener(changeListener);
}
}
public MyRadioGroupContainer(Context context) {
super(context);
}
public MyRadioGroupContainer(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyRadioGroupContainer(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public RadioGroup.OnCheckedChangeListener getCheckChangeListener() {
return mCheckChangeListener;
}
public void setCheckChangeListener(RadioGroup.OnCheckedChangeListener mCheckChangeListener) { this.mCheckChangeListener = mCheckChangeListener;
}}
2.布局文件使用View

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="@+id/fir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="1"
android:text="50元" />
android:id="@+id/sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="2"
android:text="100元" />
android:id="@+id/thir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="3"
android:text="150元" />

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="@+id/forth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="4"
android:text="200元" />
android:id="@+id/fifth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="5"
android:text="250元" />
android:id="@+id/sixth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="6"
android:text="300元" />

你可能感兴趣的:(Android实现RadioGroup的多行多列布局且互斥)