构建启用按钮的逻辑

发布于 2025-01-25 05:58:13 字数 470 浏览 2 评论 0原文

有4个按钮:2个父(S1和S2)和2个孩子(S3和S4),以及2个状态(开/关)。

  1. 如果启用了S3或S4,则会自动启用S1。
  2. 如果S1关闭并且S2已打开(反之亦然),则S3和S4仍在上面。
  3. 如果禁用父按钮,则还禁用子按钮。

使用子按钮启用S1的代码:

if (switch3.isChecked()){
           switch1.setChecked(true);
       }
       else{
           switch3.setChecked(false);
       }
        if (switch4.isChecked()){
            switch1.setChecked(true);
        }
        else{
            switch4.setChecked(false); 

There are 4 buttons: 2 parent (s1 and s2) and 2 child (s3 and s4), as well as 2 states (on/off).

  1. If s3 or s4 are enabled, then s1 is automatically enabled.
  2. If s1 is off and s2 is on (and vice versa), then s3 and s4 remain on.
  3. If the parent buttons are disabled, then the child buttons are also disabled.

Code to enable s1 using child buttons:

if (switch3.isChecked()){
           switch1.setChecked(true);
       }
       else{
           switch3.setChecked(false);
       }
        if (switch4.isChecked()){
            switch1.setChecked(true);
        }
        else{
            switch4.setChecked(false); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2025-02-01 05:58:13

让我们首先处理将使所有其他案件变得不可能的情况。在这种情况下,这将是两个父开关都关闭。这将导致两个孩子开关也关闭。然后我们检查条件1,然后最终条件2。
如果某物是true,则在其前会使它成为false是一个否定操作员,将 boolean 值转换为相反。 && and 在2个表达式之间执行的含义含义true如果和-ED 表达式值是true||在2个表达式之间执行的含义含义 true 如果两个 or-ed 表达值都是true,或1 true和另一个false。现在让我们继续。关于他们是否必须同时离开的情况,我没有得到您的条件3。但是,这个示例假设必须同时关闭这两者才能生效。

if(!s3.isChecked() && !s4.isChecked())
{
  s1.setChecked(false);
  s2.setChecked(false);
}
else if(s3.isChecked() || s4.isChecked())
{
  s1.setChecked(true);
}
else if((!s1.isChecked() && s2.isChecked()) || (!s2.isChecked() && s1.isChecked()))
{
  s3.setChecked(true);
  s4.setChecked(true);
}

执行多个操作时,最好使用Paranthesis分开每个部分。

Let us first handle the case which will make all other cases impossible. In this case, this would be if both parent switches are off. This would result in both child switches also being off. Then we check for condition 1 and then finally condition 2.
If something is true, a ! in front of it would make it a false. The ! is a negation operator, transforming a boolean value to its opposite. && is AND performed between 2 expressions meaning result will only be true if both AND-ed expression values are true. || is OR performed between 2 expressions meaning result will only be true if both OR-ed expression values are either both true, or 1 true and another false. Now let us proceed. I didn't quite get condition 3 of your question as to whether they both have to be off at the same time or not. But this example is assuming both have to be off at the same time for condition 3 to be in effect.

if(!s3.isChecked() && !s4.isChecked())
{
  s1.setChecked(false);
  s2.setChecked(false);
}
else if(s3.isChecked() || s4.isChecked())
{
  s1.setChecked(true);
}
else if((!s1.isChecked() && s2.isChecked()) || (!s2.isChecked() && s1.isChecked()))
{
  s3.setChecked(true);
  s4.setChecked(true);
}

When performing multiple operations it is best to separate each portion using paranthesis.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文