Android测验应用程序 - 确保所有问题都得到解答

时间:2014-02-14 12:04:50

标签: android checkbox radio-button

如下所示,我正在动态创建一些带有不同类型问题的测验(RadioButtons和CheckBoxes)。
所以我可以正确地生成它们并且它们应该如此出现 在问题的最后有一个普通的按钮 我想在所有问题都得到解答后才能点击它 我有一个功能,以确保所有的RadioButtons都得到了回答 但由于我有两种类型的按钮,我无法弄清楚如何确保CheckBox答案中的问题至少有1个已检查答案。

我有以下代码:

    ArrayList<RadioGroup> arrGroup;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
    buttHole = (Button) findViewById(R.id.button1); 
    buttHole.setEnabled(false);    

    getQuestions();

    //Creating the list of Questions
    LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);
    arrGroup = new ArrayList<RadioGroup>();
    try
    {

        for (int i = 0; i <= question.length; i++) 
        {
            if(question[i].multichoice == true)
            {
                TextView title2 = new TextView(this);
                title2.setText(question[i].questionName);
                title2.setTextColor(Color.BLACK);
                mLinearLayout.addView(title2);

                for(int zed = 0; zed < question[i].answers.length; zed++) 
                {               
                    CheckBox box = new CheckBox(this);
                    box.setText(question[i].answers[zed].answer);
                    box.setId(zed);
                    mLinearLayout.addView(box);
                }
            }
            else
            {
                 // create text button
                TextView title = new TextView(this);
                title.setText(question[i].questionName);                   
                title.setTextColor(Color.BLACK);
                mLinearLayout.addView(title);

                // create radio button
                final RadioButton[] rb = new RadioButton[question[i].answers.length];
                RadioGroup radGr = new RadioGroup(this);
                // take a reference of RadioGroup view
                arrGroup.add(radGr);        

                radGr.setOrientation(RadioGroup.VERTICAL);
                radGr.setOnClickListener(new OnClickListener()
                {
                    @Override
                    public void onClick(View v) 
                    {                   
                        checkQuestionsAnswer();
                    }

                });

                for (int j = 0; j < question[i].answers.length; j++) {
                    rb[j] = new RadioButton(this);
                    radGr.addView(rb[j]);
                    rb[j].setText(question[i].answers[j].answer);
                }
                mLinearLayout.addView(radGr);
            }
        }

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }    

}

public boolean checkQuestionsAnswer() 
{
    int count = arrGroup.size();

    for (int i = 0; i < count; i++) 
    {
        if (arrGroup.get(i).getCheckedRadioButtonId() == -1) 
        {
            Toast.makeText(getApplicationContext(), "Not all questions are answered!", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
    Toast.makeText(getApplicationContext(), "Thank you!", Toast.LENGTH_SHORT).show();
    buttHole.setEnabled(true);
    //Return true if all answer are given
    return true;
}

1 个答案:

答案 0 :(得分:0)

如果每个问题的答案少于10个答案,请将复选框的ID设置为(i*10)+zed。这样你就可以像这样循环遍历

for(int i=0; i<question.length; i++){

   boolean questionAnswered = false;
   for(int zed = 0; zed < question[i].answers.length; zed++) {

       CheckBox box = (CheckBox) findViewById(i*10+zed);
       if(box != null && box.isChecked()) {

           questionAnswered = true;
           ...
       }        

   }

}

我没有尝试这个代码,这只是一个快速而肮脏的提示,但我希望它有所帮助。