1. 程式人生 > >自定義RadioGroup動態新增RadioButton,並獲取選中radioButton的位置

自定義RadioGroup動態新增RadioButton,並獲取選中radioButton的位置

一,自定義的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>