构建启用按钮的逻辑
有4个按钮:2个父(S1和S2)和2个孩子(S3和S4),以及2个状态(开/关)。
- 如果启用了S3或S4,则会自动启用S1。
- 如果S1关闭并且S2已打开(反之亦然),则S3和S4仍在上面。
- 如果禁用父按钮,则还禁用子按钮。
使用子按钮启用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).
- If s3 or s4 are enabled, then s1 is automatically enabled.
- If s1 is off and s2 is on (and vice versa), then s3 and s4 remain on.
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让我们首先处理将使所有其他案件变得不可能的情况。在这种情况下,这将是两个父开关都关闭。这将导致两个孩子开关也关闭。然后我们检查条件1,然后最终条件2。
如果某物是
true
,则!
在其前会使它成为false
。!
是一个否定操作员,将 boolean 值转换为相反。&&
是 and 在2个表达式之间执行的含义含义true
如果和-ED 表达式值是true
。||
是或在2个表达式之间执行的含义含义 true 如果两个 or-ed 表达值都是true
,或1true
和另一个false
。现在让我们继续。关于他们是否必须同时离开的情况,我没有得到您的条件3。但是,这个示例假设必须同时关闭这两者才能生效。执行多个操作时,最好使用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 afalse
. The!
is a negation operator, transforming a boolean value to its opposite.&&
is AND performed between 2 expressions meaning result will only betrue
if both AND-ed expression values aretrue
.||
is OR performed between 2 expressions meaning result will only betrue
if both OR-ed expression values are either bothtrue
, or 1true
and anotherfalse
. 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.When performing multiple operations it is best to separate each portion using paranthesis.