如何确保用户是否检查了所有单选按钮?

时间:2014-02-04 09:35:33

标签: android if-statement radio-group

所以我正在尝试制作一个简单的测验应用程序,我想确保用户在继续下一个活动之前回答所有问题。在开始时,我设置的按钮不可用,我想只在所有问题都已被回答的情况下使其可用。我无法弄清楚在哪里以及如何检查所有问题是否都有答案。

public class Quiz extends Activity 
{
Button buttHole;


String countryName[] = { "India", "Pakistan", "China", "Nepal", "Pichmaala",  "Blah" };

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

    buttHole.setEnabled(false);

    //Creating the list of Questions
    LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);

      for (int i = 1; i <= 3; i++)        
      {
       //create text button
       TextView title = new TextView(this);
       title.setText("Question Number:" + i);
       title.setTextColor(Color.GREEN);
       mLinearLayout.addView(title);

       // create radio button
       final RadioButton[] rb = new RadioButton[countryName.length];
       RadioGroup radGr = new RadioGroup(this);
       radGr.setOrientation(RadioGroup.VERTICAL);
       radGr.setOnClickListener(l)

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



}

}

2 个答案:

答案 0 :(得分:0)

尝试使用以下代码检查用户是否已回答所有问题

第一个解决方案

int count = mLinearLayout.getChildCount();

for(int i=1;i<count ;i+=2){
    RadioGroup rg = (RadioGroup) mLinearLayout.getChildAt(i);
    if(rg.getCheckedRadioButtonId()==-1){
        //Answer is not selected
        //Do something to prevent execution
        break;
    }
}

第二个解决方案

public class Quiz extends Activity {
Button buttHole;

String countryName[] = { "India", "Pakistan", "China", "Nepal",
        "Pichmaala", "Blah" };

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

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

    for (int i = 1; i <= 3; i++) {
        // create text button
        TextView title = new TextView(this);
        title.setText("Question Number:" + i);
        title.setTextColor(Color.GREEN);
        mLinearLayout.addView(title);

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

        radGr.setOrientation(RadioGroup.VERTICAL);
        radGr.setOnClickListener(l);

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

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

    for (int i = 0; i < count; i++) {
        if (arrGroup.get(i).getCheckedRadioButtonId() == -1) {
            //Return false, answer is remaining to given
            //You can pass here position also to know question number
            return false;
        }
    }
    //Return true if all answer are given
    return true;
}
}

答案 1 :(得分:0)

同时给出radiobuttons id's,当你要提交这个表格时(例如)意味着点击按钮,按id获取单选按钮对象,然后检查radiobutton.isChecked()。