2014-02-17 118 views
1

嗨我正在做某种测验应用程序,我想确保用户检查至少1个复选框。如何确保至少有一个复选框被选中?

我创建了复选框这样的:

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++) 
       {    
        final CheckBox box = new CheckBox(this); 
        box.setText(question[i].answers[zed].answer); 
        box.setId(zed);      
        mLinearLayout.addView(box); 
        int flag = 0; 
        if(box.isChecked() == true) 
        { 
         flag = 1; 
        } 
        if(flag == 1) 
        { 
         Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_SHORT).show(); 
        } 
       }     
      } 
      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(); 
    } 

但我无法弄清楚如何确保用户已至少检查1盒。

+0

保持乌尔复选框refrence列表,并通过对象迭代,并请检查是否isChekced – CoolMonster

回答

0

加入您的当您创建它们时,复选框会显示在列表中。然后在循环中使用

checkbox.isChecked() 

例程,以确保至少检查了1个。

看看这里的复选框API

http://developer.android.com/reference/android/widget/CheckBox.html

和这里的列表:

http://developer.android.com/reference/java/util/List.html \

// Added List 
List<Checkbox> ansList = new List<Checkbox>(); 

// YOUR CODE with the list addition 
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++){ 

      final CheckBox box = new CheckBox(this); 
      box.setText(question[i].answers[zed].answer); 
      box.setId(zed); 

      // Since a test, make sure no answer is checked 
      box.setChecked(false); 

      // Add the checkbox to YOUR list of boxes 
      ansList.add(box); 

      mLinearLayout.addView(box);      

     } 
    } 
} 


// Later you can check with this 
boolean atLeastOne = false; 
for (int i = 0; i < question.length ; i++){ 

    if(question[i].multichoice == true){ 

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

      if(ansList.get(zed).isChecked()) atLeastOne = true;      

     } 
    } 
} 

if(true == atLeastOne){ 
    // Do whatever you want to do if at least one is checked 
} 

我还没有实际运行,或者选中该代码,但是这应该让你开始,它应该工作。

+0

您确定这是制作列表的正确方法,因为出现此错误:“无法实例化类型列表” – user3182266

+0

@ user3182266 - 是的,我曾说过我没有尝试过。您必须定义需要放入列表中的什么类型的数据元素。尝试新编辑的答案代码,因为它在ansList声明之后添加了“”。 – trumpetlicks

+0

nope仍然出现错误“列表不能解析为类型” – user3182266

0

只是做一个标记,并检查每一个时间,如果一个复选框是真的让它=“1”,最后检查是否为“1”或不 喜欢的东西

Box[0]=name_of_first_Checkbox; 
Box[1]=name_of_second_Checkbox; 
// and so on.. 
int flag = 0; 
for (int i = 0; i < x ; i++){ 
      if(Box[i].isChecked()) 
      {    
       flag =1; 
      } 
} 

if(flag==1){ 
    //At least 1 box is checked 
} 
+0

盒是不是数组 – user3182266

+0

这仅仅是你可以把值,而不是说一个例子,等待我会更新我的答案 –

+0

@ user3182266有你走!如果我澄清了你的疑问,请在我的回答中打勾 –

0

我给一个快速的逻辑阅读您的问题之后到来,

  • 创建一个名为isCheckButtonClicked
  • 一类级别布尔变量默认情况下,每个复选框的Click事件将其值设置为false现在
  • 设置它的值为真
  • 现在在最后如果您发现它的值为true,意味着至少有一个复选框被剔除
1

也许你可以尝试这种方法?

private boolean checkSelectionExists() { 
    boolean selectionExists = false; 
    selectionExists = (boolean) ((CheckBox) findViewById(R.id.main_checkbox1)).isChecked() ? true : false || 
      (boolean) ((CheckBox) findViewById(R.id.main_checkbox2)).isChecked() ? true : false || 
      (boolean) ((CheckBox) findViewById(R.id.main_checkbox3)).isChecked() ? true : false; 

    return selectionExists; 
} 
相关问题