如何动态地使用单选组和文本视图创建测验部分?

how to create quiz section using radio group and textview dynamically?

“我想在片段选项卡中创建一个测验部分,结构应如下所示

问题1

无线电组 1

RadioButton1

RadioButton2

单选按钮 3

RadioButton4

问题2

无线电组 2

RadioButton5

RadioButton6

RadioButton7

RadioButton8

问题3

无线电组 3

RadioButton9

RadioButton10

RadioButton11

RadioButton12

使用下面的代码我可以只创建一个问题和一个单选按钮,我想在将来让它动态化

private void creatRadioButtons() {
        group = new RadioGroup(this.getContext());
       // group = (RadioGroup) view.findViewById(R.id.radioGroup);
        int[] panels = getResources().getIntArray(R.array.no_of_solar_panels);
        int no_of_que = 2;

       for(int i = 0; i < no_of_que; i++){
          // textView = (TextView) view.findViewById(R.id.question);
           textView = new TextView(this.getContext());
           textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
           textView.setText("Question number " + i);
           linearLayout.addView(textView);
           for (int j = 0;  j< panels.length; j++){
               button = new RadioButton(this.getContext());
               button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
               button.setText(String.valueOf(panels[j]));
               group.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT));
               group.addView(button);

           }
           linearLayout.addView(group);

       }


    }

” This is how my screen is coming up now

您应该使用 RecyclerView 而不是在设计时仅添加固定数量的问题,通过 RecyclerView 您可以动态添加任意数量的问题。

您需要使用recyclerview创建动态号码。的问题。在回收站布局中,创建一个带有四个单选按钮的问题。

有关回收器示例,请参阅 this

试试这个我已经做了测验类型的应用程序,问题类型填空,匹配以下内容,复选框,单选按钮

This is activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/rel_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/questionsLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


    </LinearLayout>

</ScrollView>

</RelativeLayout>

This is my QuestionChoiceVo.java (This class to hold question and it's choices)

import java.io.Serializable;
import java.util.ArrayList;


public class QuestionChoiceVo implements Serializable {

String question;

ArrayList<String> choiceArrayList;

public QuestionChoiceVo(String question, ArrayList<String> choiceArrayList) 
{
    this.question = question;
    this.choiceArrayList = choiceArrayList;
}

public String getQuestion() {
    return question;
}

public void setQuestion(String question) {
    this.question = question;
}

public ArrayList<String> getChoiceArrayList() {
    return choiceArrayList;
}

public void setChoiceArrayList(ArrayList<String> choiceArrayList) {
    this.choiceArrayList = choiceArrayList;
}
}

This is my MainActivity class.

public class MainActivity extends AppCompatActivity {

LinearLayout linearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    initialiseView();

}

private void initialiseView() {

    linearLayout = (LinearLayout) findViewById(R.id.questionsLinearLayout);

    ArrayList<QuestionChoiceVo> questionChoiceVoArrayList = new ArrayList<>();

    QuestionChoiceVo mQuestionChoiceVoOne = new QuestionChoiceVo("Question One", new ArrayList<String>() {{add("Choice One");add("Choice Two");add("Choice Three");}});
    QuestionChoiceVo mQuestionChoiceVoTwo = new QuestionChoiceVo("Question Two", new ArrayList<String>() {{add("Choice One");add("Choice Two");add("Choice Three");}});
    QuestionChoiceVo mQuestionChoiceVoThree = new QuestionChoiceVo("Question Three", new ArrayList<String>() {{add("Choice One");add("Choice Two");add("Choice Three");}});

    questionChoiceVoArrayList.add(mQuestionChoiceVoOne);
    questionChoiceVoArrayList.add(mQuestionChoiceVoTwo);
    questionChoiceVoArrayList.add(mQuestionChoiceVoThree);

    prepareQuestionAnswerLayout(questionChoiceVoArrayList);

}

private void prepareQuestionAnswerLayout(ArrayList<QuestionChoiceVo> questionChoiceVoArrayList) {

    for (QuestionChoiceVo mQuestionChoiceVo : questionChoiceVoArrayList) {

        LinearLayout mSingleQuestionLinearLayout = new LinearLayout(this);

        mSingleQuestionLinearLayout.setOrientation(LinearLayout.VERTICAL);

        mSingleQuestionLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        TextView mTextView = new TextView(this);

        mTextView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        mTextView.setText(mQuestionChoiceVo.getQuestion());

        mTextView.setTextSize(20f);

        mSingleQuestionLinearLayout.addView(mTextView);

        RadioGroup mChoiceRadioGroup = setUpChoices(mQuestionChoiceVo);

        RelativeLayout.LayoutParams radioGroupLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        mChoiceRadioGroup.setLayoutParams(radioGroupLayoutParams);

        mSingleQuestionLinearLayout.addView(mChoiceRadioGroup);

        linearLayout.addView(mSingleQuestionLinearLayout);
    }

}

private RadioGroup setUpChoices(QuestionChoiceVo mQuestionChoiceVo) {

    RadioGroup radioGroup = new RadioGroup(this);

    radioGroup.setId(View.generateViewId());

    for (int i = 0; i < mQuestionChoiceVo.getChoiceArrayList().size(); i++){

        RadioButton radioButton = new RadioButton(this);

        radioButton.setText(mQuestionChoiceVo.getChoiceArrayList().get(i));

        radioButton.setTextSize(18f);

        RadioGroup.LayoutParams radioGroupLayoutParams = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        radioGroupLayoutParams.setMargins(10, 10, 10, 10);

        radioButton.setPadding(10, 10, 10, 10);

        radioButton.setLayoutParams(radioGroupLayoutParams);

        radioButton.setId(View.generateViewId());

        radioGroup.addView(radioButton);

    }

    return radioGroup;
}


}

Output As Your Requirement :

请试试这个代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="@+id/parentLayout"/>
</ScrollView>

MainActivity.java

public class MainActivity extends AppCompatActivity {

private LinearLayout parentLayout;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=MainActivity.this;
    parentLayout=findViewById(R.id.parentLayout);
    createRadioButtons();

}
private void createRadioButtons(){
    //Number pf questions
    int[] panels = new int[]{1,2,3,4,5};
    int no_of_que = 20;

    TextView question;
    RadioGroup radioGroup;
    RadioButton radioButton;
    for (int i=0;i<no_of_que;i++){
        question = new TextView(context);
        question.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        question.setText("Question: "+i);
        parentLayout.addView(question);

        radioGroup=new RadioGroup(context);
        radioGroup.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        parentLayout.addView(radioGroup);

        for(int j=0;j<panels.length;j++){
            radioButton=new RadioButton(context);
            radioButton.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));;
            radioButton.setText("Radio Button");
            radioGroup.addView(radioButton);
        }
    }
}
   }