Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何确保选中至少一个复选框?_Android_Checkbox - Fatal编程技术网

Android 如何确保选中至少一个复选框?

Android 如何确保选中至少一个复选框?,android,checkbox,Android,Checkbox,嗨,我正在做一些测验应用程序,我想确保用户选中至少一个复选框 我正在创建以下复选框: LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1); arrGroup = new ArrayList<RadioGroup>(); try { for (int i = 0; i < question.length ; i++) {

嗨,我正在做一些测验应用程序,我想确保用户选中至少一个复选框

我正在创建以下复选框:

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();
    } 
LinearLayout mLinearLayout=(LinearLayout)findViewById(R.id.linear1);
arrGroup=newarraylist();
尝试
{
for(int i=0;i

但我不知道如何确保用户至少选中了1个复选框。

只要做一个标记,每次都选中,如果复选框为true,则将其设置为=“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
}
Box[0]=第一个复选框的名称;
框[1]=第二个复选框的名称;
//等等。。
int标志=0;
对于(int i=0;i
在创建复选框时将其添加到列表中。然后使用

checkbox.isChecked()
循环中的例程,以确保至少检查了1

在这里查找checkbox API

下面是一份清单:

\

//添加的列表
List ansList=新列表();
//您的代码与列表添加
for(int i=0;i

我还没有实际运行或检查过这段代码,但这应该可以让您开始,而且它应该可以工作。

在阅读您的问题之后,我给出了一个快速的逻辑

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

    • 也许你可以试试这个方法

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

      在列表中保留您的复选框引用,并遍历对象,检查IschekEdit是否只是一个示例,您可以将值替换为其他值,等等,我将更新我的answer@user3182266好了!如果我澄清了你的疑问,请在我的回答旁边打勾。嗯,还是没什么。。。我不知道,但在第一个if之后我无法访问值标志statment@user3182266你不能访问它是什么意思?隐马尔可夫模型?你正确申报了吗。。?试着制作一个新的短程序来尝试这个,然后试着把它放到你的主代码中…好的,我做了一些类似的事情,我在for循环中声明了值,并在flag中添加了1,然后我做了一个if statment if flag是1来打印一个toast,它不会打印它。你确定这是制作列表的正确方法吗,因为我遇到了这个错误
      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;
      }