如何确保至少选中了1个复选框?

编程入门 行业动态 更新时间:2024-10-23 19:28:41
本文介绍了如何确保至少选中了1个复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在制作某种测验应用程序,并且我想确保用户选中至少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个复选框?

本文发布于:2023-11-04 01:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1556527.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!