首页 > 解决方案 > 检查单击复选框的条件

问题描述

我有 3checkboxesactivity。我需要检查checkbox(单击/未选中)的状态,并根据此结果执行特定操作。我在这项工作中的逻辑如下:

if(Check1.isCheked) {
    //some action
} else if(Check2.isCheked) {
    //some action
} else if (Check3.isCheked) {
    //some action
} else if ((Check1.isCheked) && (Check2.isCheked)) {
    //some action
} else if ((Check1.isCheked) && (Check3.isCheked)) {
    //some action
} // etc.

如果有一些更合理的方法,例如,因为我有 9checkboxes并且为了他们的验证,我需要 509 条件?

我的代码中这种情况的一个具体例子(这是我写的):

if (some_variable  == 1) {
        Intent intent = new Intent(this, activity.class);
        intent.putExtra("1", mCheck1.isChecked());
        intent.putExtra("2", (mCheck1.isChecked()) && (mCheck2.isChecked()));
        //and other 507 variants
        startActivity(intent);
} else if (some_variable == 2) {
        Intent intent1 = new Intent(this, activity.class);
        intent.putExtra("3", mCheck1.isChecked());
        intent.putExtra("4", (mCheck1.isChecked()) && (mCheck2.isChecked()));
        //and other 507 variants
        startActivity(intent1);
}

在接下来activity的一个得到这个检查的结果:

Boolean check1 = getIntent().getExtras().getBoolean("1");
Boolean check2 = getIntent().getExtras().getBoolean("2");

Boolean check3 = getIntent().getExtras().getBoolean("3");
Boolean check4 = getIntent().getExtras().getBoolean("4");

然后我开始for循环检查这个结果:

if(check1) {
//query the database
} else if (check2) {
//query the database
} else if...

标签: javaloopsfor-loopif-statementwhile-loop

解决方案


如果我的要求有误,请编辑您的问题,通过提供示例来阐明您在每个 if 语句中所做的工作)

如果我得到您的最新评论并且您在每个 is 语句中所做的只是在您的via上设置一个Boolean值,您可以使用 a来表示选中了哪些复选框。IntentputExtrabit mask

每个复选框都有一点:

000000000 if nothing is checked  
000000001 if only the 1st checkbox is checked 
001001000 if 4th and 7th checkbox is checked 
etc..

从这些二进制数中,您可以得到每个可能组合的 int 表示。IE:

int intRepresentation = 0;
if(checkbox1Checked) intRepresentation += 1;
if(checkbox2Checked) intRepresentation += 2;
if(checkbox3Checked) intRepresentation += 4;
if(checkbox4Checked) intRepresentation += 8;
if(checkbox5Checked) intRepresentation += 16;
if(checkbox6Checked) intRepresentation += 32;
if(checkbox7Checked) intRepresentation += 64;
if(checkbox8Checked) intRepresentation += 128;
if(checkbox9Checked) intRepresentation += 256;

然后,您只需要创建一个整数列表,表示您要为其添加 true 的所有组合。例如,如果要将以下 3 种组合的值设置为 true:

checkbox1 (0000001 = 1)
checkbox3 and checkbox 5 (000010100 = 20)
checkbox6 (000100000 = 64)

你列出了这个清单:

List<Integer> myList = Arrays.asList(1, 20, 64)

然后

int intRepresentation = // whatever value you get from your selected checkboxes

myIntent.putExtra("VALUE_NAME", myList.contains(intRepresentation))

编辑:

因此,鉴于您的具体示例,我有另一个想法来提取所有 && 条件并具有易于阅读的图形表示

您可以将所有组合描述为字符串,其中每个字符代表复选框的状态,“o”选中,“x”未选中,“-”忽略。以下是示例:

"o--------", // check1 is checked (ignore others)
"oo-------", // check1 and check2 are checked (ignore others)
"o--o-x---"  // check1 and check4 are checked and check6 is unchecked (ignore others)

然后你用它做一个数组

String[] patterns = new String[] {
    "o--------", 
    "oo-------", 
    "o--o-x---" ,
    // and 506 other ones
    };

然后获取你的复选框的状态,假设1、4、5被勾选,你的实际情况是"oxxooxxxx"

然后您可以定义一个方法来查看您的实际字符串是否与模式匹配

private boolean match(String actualPattern, String patternToMatch) {
    boolean match = true;
    for(int i=0; i<patternToMatch.length(); i++) {
        if(patternToMatch.charAt(i) != '-'
        && patternToMatch.charAt(i) != actualPattern.charAt(i)) {
            match = false;
        }
    }
    return true;
}

鉴于以下情况"oxxooxxxx",它将匹配模式 [0] ( "o--------"),因为复选框 1 被选中,模式 [2] ( "o--o-x---") 因为复选框 1 和 4 被选中,而 6 未被选中

最后以这种方式更改您的代码:

Intent intent = new Intent(this, activity.class);

if (some_variable  == 1) {
    intent.putExtra("1", match(stringRepresentation, patterns[0]));
    intent.putExtra("2", match(stringRepresentation, patterns[1]));
    //and other 507 variants
} else if (some_variable == 2) {
    intent.putExtra("3", match(stringRepresentation, patterns[0]));
    intent.putExtra("4", match(stringRepresentation, patterns[1]));
    //and other 507 variants
}

startActivity(intent);

进一步简化:

如果您的 ifs 中的代码看起来是相同的,并且只有额外的名称不同,您可以通过以下方式替换所有 ifs 来进一步简化代码:

制作一个地图,列出 some_variable 的每个可能值的附加名称。在您上面编写的示例中,它看起来像这样。

Map<Integer, String[]> extraNames = new HashMap<Integer, String[]>() {{
    put(1, new String[] {"1", "2"});
    put(2, new String[] {"3", "4"});
}};

然后可以删除所有 ifs,您可以通过以下方式替换它们:

intent.putExtra(extraNames[some_variable][0], match(stringRepresentation, patterns[0]));
intent.putExtra(extraNames[some_variable][1], match(stringRepresentation, patterns[1]));
//etc .. 

它行得通,我想我们甚至可以更进一步,写

for(int i=0; i<extraNames.keySet().size(); i++) {
    intent.putExtra(extraNames[some_variable][i], match(stringRepresentation, patterns[i]));
}

推荐阅读