Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.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 当复选框中的多个复选框中的复选框1+2被选中时,会出现此代码_Android_Checkbox - Fatal编程技术网

Android 当复选框中的多个复选框中的复选框1+2被选中时,会出现此代码

Android 当复选框中的多个复选框中的复选框1+2被选中时,会出现此代码,android,checkbox,Android,Checkbox,代码有什么问题..无论我如何更改值..它仍然在3个复选框中显示为负数而不是正数假设您已经正确实现了单选或多选的CheckableLayout,您可以像下面那样调整代码以实现您可能想要的效果 if (q1.isChecked()) { int count = 0; for (CheckBox cb : cbList) { if (cb.isChecked()) { count++;

代码有什么问题..无论我如何更改值..它仍然在3个复选框中显示为负数而不是正数

假设您已经正确实现了单选或多选的CheckableLayout,您可以像下面那样调整代码以实现您可能想要的效果

    if (q1.isChecked()) {
        int count = 0;
        for (CheckBox cb : cbList) {
            if (cb.isChecked()) {
                count++;
            }
        }
        if (count <= 2) {
            //this Toast will show when only 1 or 2 checkbox will be checked
            Toast.makeText(getApplicationContext(), getString(R.string.negative), Toast.LENGTH_SHORT).show();
        } else {
            if(count > 2) {
        }
            //this Toast will show when more than 2 checkboxs will be checked
            Toast.makeText(getApplicationContext(), getString(R.string.positive), Toast.LENGTH_SHORT).show();
        }
    }

我给你这个答案的依据是我从你的问题中了解到的…可能是你在寻找这种类型的解决方案

...
 // Looping if CheckBox number 1 is not selected and the others are not
 // Get an array that tells us for each position whether the item is
 // checked or not
 // --

final SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
        if (checkedItems.size() == 0) {
            Toast.makeText(this, "No selection info available", Toast.LENGTH_LONG).show();
            return;
        }

你的问题不够清楚。@Hamid..I这里有个错误}否则计数>2{我在else之后添加if来解决问题,但是当我选中3个复选框时显示为负@hamidcheck out在选中3个复选框后包含什么值count。代码有什么问题..无论我如何更改值..它仍然在3个复选框中显示为负而不是正..我更新了上面的代码..你能帮我修复它吗?@hamidsorry,那里我的代码中有一个错误…请检查更新并将其粘贴到您的类中…很遗憾,我在else之后添加了一个无效的条件。现在,我删除了该条件。
if (q1.isChecked()) {
    int count = 0;
    for (CheckBox cb : cbList) {
        if (cb.isChecked()) {
            count++;
        }
    }
    if (count <= 2) {
        //this dialog will show when only 1 or 2 checkbox will be checked
        new AlertDialog.Builder(this).setMessage(getString(R.string.negative)).show();
    } else {
        //this dialog will show when more than 2 checkboxs will be checked
        new AlertDialog.Builder(this).setMessage(getString(R.string.positive)).show();
    }
}
if (q1.isChecked()) {
    int count = 0;
    for (CheckBox cb : cbList) {
        if (cb.isChecked()) {
            count++;
        }
    }
    if (count <= 2) {
        //this Toast will show when only 1 or 2 checkbox will be checked
        Toast.makeText(getApplicationContext(), getString(R.string.negative), Toast.LENGTH_SHORT).show();
    } else {
        //this Toast will show when more than 2 checkboxs will be checked
        Toast.makeText(getApplicationContext(), getString(R.string.positive), Toast.LENGTH_SHORT).show();
    }
}