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

Android 如何确保用户检查了所有单选按钮?,android,if-statement,radio-group,Android,If Statement,Radio Group,因此,我正在尝试制作一个简单的测验应用程序,我想确保用户在继续下一个活动之前回答了所有问题。开始时,我设置的按钮不可用,我只想在所有问题都已回答的情况下使其可用。我不知道在哪里以及如何检查所有的问题是否都有答案 public class Quiz extends Activity { Button buttHole; String countryName[] = { "India", "Pakistan", "China", "Nepal", "Pichmaala", "Blah" };

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

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



}
公共课堂问答扩展活动
{
按钮对接孔;
字符串countryName[]={“印度”、“巴基斯坦”、“中国”、“尼泊尔”、“皮奇马拉”、“布拉赫”};
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u测验);
对接孔=(按钮)findViewById(R.id.button1);
对接孔。设置启用(错误);
//创建问题列表
LinearLayout mLinearLayout=(LinearLayout)findViewById(R.id.linear1);

对于(inti=1;i请尝试以下代码,以检查用户是否已给出所有问题的答案

第一个解决方案

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;
}
}
int count=mLinearLayout.getChildCount();

对于(int i=1;i也给出单选按钮id,并且当您准备提交此表单时(例如)表示单击按钮时,按id获取单选按钮对象,然后选中radiobutton.isChecked()。

您必须使用
getCheckedRadioButtonId()获取每个单选组的选定单选按钮
。如果未选择按钮,则返回-1。我不知道您在做什么。但我检查了我的第二个解决方案。该解决方案根据您的要求运行得非常完美。如果问题的任何答案仍然存在,它将为您提供
false
。这是您的问题
。我不知道在哪里以及如何检查所有问题是否都存在有一个答案
。它在我的答案中工作得非常完美。如果方法return
false
意味着还有多个答案需要回答。而
true
意味着,所有答案都给出了。嗯,好吧,这是一个非常有趣的问题。让我想想。我应该在哪里调用checkQuestionsAnswer()方法?我在for循环外的onCreate方法中对其进行冷却是否正确?如果我不调用它,代码将永远无法到达它。此外,我看到你有onClickListener“l”,你在哪里声明“l”变量,因为我在你建议的解决方案中没有看到它的声明。我已删除了radGr.setOnClickListener(l);因为它没有在任何地方申报,也没有使用。我非常抱歉,但这个问题整天困扰着我,而且越来越困扰我nerves@user3182266你不觉得我的答案很有用吗?使用你的代码,程序在RadioGroup rg=(RadioGroup)mLinearLayout.getChildAt(i);//源代码不是found@user3182266是否可以放置完整的堆栈跟踪(日志)对于this.java.lang.ClassCastException:android.widget.TextView不能强制转换为android.widget。RadioGroup@user3182266,哦,对不起。我犯了一个错误。您先输入了
TextView
,然后输入了
RadioGroup
。这是我的错误。让我来解决这个问题。请耐心等待……)@用户3182266是否有一个子XML,该子XML在您添加标题和
RadioGroup
时仍然存在?因此,该子XML将索引为“0”,标题为“1”/“3”/“5”/…而
RadioGroup
将为“2”/“4”/“6”/…如果没有,请尝试清理您的项目,因为根据您提供的代码,Chintan Rathod的(第一个)解决方案应该可以工作;)