一,自定义的radioGroup,根据项目需求,需要实现考试试卷中单选题形式,其中,在选项前面要有正确答案的提示,例如:(答案)A 选项内容 ,且前面(答案)隐藏,在返回上一题时,正确答案前的这个标志会显示出来,所以需要我们自定义控件继承radiogroup,其中每行包含textview ,radiobutton
代码如下:
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;
public class MultipleRadioGroup extends RadioGroup {
private OnCheckedChangeListener mOnCheckedChangeListener;
public MultipleRadioGroup(Context context) {
super(context);
}
public MultipleRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
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(MultipleRadioGroup.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);
}
}
}
}
}
}
}
二,实现动态添加该自定义控件的每项。
代码实现如下:
@BindView(R.id.radioGroup)MultipleRadioGroup radio;
int[] drawable = {R.drawable.selector_examination_paper_a, R.drawable.selector_examination_paper_b,
R.drawable.selector_examination_paper_c, R.drawable.selector_examination_paper_d,
R.drawable.selector_examination_paper_d, R.drawable.selector_examination_paper_d};
for (int i = 0; i < drawable.length; i++) {
view = getLayoutInflater().inflate(R.layout.multiple_radio_group_item, null);
radioButton = (RadioButton) view.findViewById(R.id.radio_item);
radioButton.setButtonDrawable(drawable[i]);
radio.addView(view);
final int finalI = i;
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
checkId=finalI;
} }
});
}
其中drawable对应的radiobutton的图片用来显示abcdefg这种选项, 每项item的布局对应是multiple_radio_group_item.xml文件。
checkID对应就是用户选择第几个答案的值,注意是从0开始的。
三,附xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView
android:id="@+id/radio_answer"
android:layout_width="50dp"
android:layout_height="40dp"
android:gravity="center"
android:text="" />
<RadioButton
android:id="@+id/radio_item"
android:layout_width="match_parent"
android:layout_height="40dp"
android:button="@null"
android:text="这里输入选项" />
</LinearLayout>