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
<?xml version="1.0" encoding="utf-8"?>
<com.lance.frame.view.MyRadioGroupContainer xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_my_account_radiocontainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/fir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="1"
android:text="50元" />
<RadioButton
android:id="@+id/sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="2"
android:text="100元" />
<RadioButton
android:id="@+id/thir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="3"
android:text="150元" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/forth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="4"
android:text="200元" />
<RadioButton
android:id="@+id/fifth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="5"
android:text="250元" />
<RadioButton
android:id="@+id/sixth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="6"
android:text="300元" />
</RadioGroup>
</com.lance.frame.view.MyRadioGroupContainer>
网友评论