2014-02-14 57 views
0

正如您在下面看到的,我正在動態創建一些包含不同類型問題(RadioButtons和CheckBoxes)的測驗。
因此,我可以正確生成它們,並且它們應該顯示。
在問題結尾處有一個正常的Button。
而且我希望只有在所有問題都得到解答後纔可以點擊。
我有一個函數來確保所有的RadioButton都被回答。
但是,由於我有兩種類型的按鈕,我無法弄清楚如何確保CheckBox問題的答案至少有一個檢查答案。Android測驗應用程序 - 確保回答所有問題

我有以下代碼:

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; 
} 

回答

0

如果每個問題的複選框的ID設置爲(i*10)+zed例如小於10分的答案。這樣,你可以通過回答環路這樣

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; 
      ... 
     }   

    } 

} 

我沒有嘗試這個代碼,它只是一個快速和骯髒的暗示,但我希望它能幫助。

+0

nope它沒有工作出於某種原因:/不知道爲什麼 – user3182266

+0

你能夠調試它?什麼不工作? –