我正在制作某种测验应用程序,并且我想确保用户选中至少1个复选框.
Hi I am making some sort of quiz application and I want to make sure the user checks at least 1 check box.
我正在创建如下复选框:
I am creating the the check boxes like this:
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个框.
But I cant figure out how to make sure the user has checked at least 1 box.
推荐答案在创建复选框时将其添加到列表中.然后使用
Add your checkboxes to a list as you create them. then use the
checkbox.isChecked()循环执行例程,以确保至少检查了1个.
routine in a loop to make sure at least 1 is checked.
在此处查找复选框API
Look here for the checkbox API
developer.android/reference/android/widget/CheckBox.html
和此处的列表:
developer.android/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 }我还没有真正运行或检查过此代码,但这应该可以使您入门,并且应该可以正常工作.
I have NOT actually run or checked this code, but this should get you started, and it SHOULD work.
更多推荐
如何确保至少选中了1个复选框?
发布评论