【问题标题】:Checking the condition of clicking on the checkbox检查单击复选框的条件
【发布时间】:2018-10-09 05:23:56
【问题描述】:

我的activity 上有 3 个checkboxes。我需要检查checkbox(clicked/unclecked) 的状态,并根据此结果执行特定操作。 我在这项工作中的逻辑如下:

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.

如果有一些更合理的方法,例如,因为我有 9 个checkboxes 并且为了他们的验证,我需要 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...

【问题讨论】:

  • 条件是否总是检查isChecked 还是有时会有所不同?
  • 不,只检查isCheked@Aominè
  • 您绝对可以创建一种方法来帮助减少&& 条件,但我想不出避免ifelse if 等的方法。
  • if 块中每个 ifelse if 是否有相似之处?
  • 我也想不通)@Aominè 所以我写了所有的 509 条件,但在这种情况下真的都是这样吗..

标签: java loops for-loop if-statement while-loop


【解决方案1】:

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

如果我的最新评论正确,并且您在每个语句中所做的只是通过putExtra 在您的Intent 上设置Boolean 值,您可以使用bit mask 来表示选中了哪些复选框。

每个复选框都有一点:

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

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

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

然后获取你的checkbox的状态,假设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]));
}

【讨论】:

  • 你让我吃惊@Bentaye,老实说我从来没有遇到过bitmask,我更新了问题以明确我想要实现的目标
  • 如果我理解的话,无论如何我必须添加我选择的所有 509 个变体,这并不能简化我的任务。但我读得更高,我写的那个声明不起作用。我需要选择删除我所有的工作,然后重新开始,或者我可以继续,请记住你对未来的建议@Bentaye
  • @SpikeJohnson 我添加了一个带有其他想法的编辑,这可能对您的具体示例有用。我就是这样做的,因为您可以以图形方式编写您的 509 组合,并且很容易将它们可视化并在需要时更改它们。
  • 非常感谢@Bentaye,这是非常有趣的方式)以及重构呢,我可以为您提供所需的所有信息。
  • @SpikeJohnson 我的意思是在您的示例中,ifs 中的代码是相同的,只是附加的名称发生了变化。如果 t 是这种情况并且 509 条件在两种情况下都相同,那么您可以动态生成额外的名称,并且只在 if 中编写一次代码。
猜你喜欢
  • 1970-01-01
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
相关资源
最近更新 更多