仅在独立检查/激活两个开关按钮时才做某事
Only do something when two Switch Buttons are independently checked / activated
我有两个触发某些操作的开关按钮。如果我激活 A,动作 A 会发生,如果我激活 B,动作 B 会发生,但如果我同时激活 A 和 B,则什么也不会发生。这是我的代码:
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggled_if_debug_activated();
checker1 = true;
}
else {
normal_main_list();
}
}
});
switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggled_if_image_mode_activated();
checker2 = true;
}
else {
normal_main_list();
}
}
});
我尝试了很多方法,但没有任何效果。如何添加仅在两个开关都打开时才触发的动作 C(例如 toggled_if_all_checked)?据我研究 .setOnCheckedChangeListener 不支持任何类似 (switch0 && switch1).setOnCheckedChangeListener(...) 的东西。那么我该怎么做呢?
在此先感谢您,愿您的道路通向温暖的沙滩!
================================ 编辑 - 解决方案 ========== ==================
Liar 的解决方案几乎完美无缺,但我有一些错误,分别在激活两个开关并尝试仅停用其中一个时,输出有点错误。更好的解释:
You activate Switch A => Output A; You activate Switch B => Output B;
You activate Switch A && B => Output C; You deactivate either A or B
and the program was showing Outputs B or A (Basically upside down
working)
这是我想出的代码:
switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked && switch1.isChecked())
{
toggle_C();
}
else
if(isChecked && !switch1.isChecked())
{
toggle_A();
}
else
if (!isChecked && switch1.isChecked())
{
toggle_B();
}
else {
toggle_DEFAULT();
}
}
});
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked && switch0.isChecked())
{
toggle_C();
}
else
if(isChecked && !switch0.isChecked())
{
toggle_B();
}
else
if (!isChecked && switch0.isChecked())
{
toggle_A();
}
else {
toggle_DEFAULT();
}
}
});
也许这不是最专业的解决方案,但它工作正常并且没有错误。感谢所有帮助过我的人!愿你的道路将你引向温暖的沙滩
您可以像这样检查另一个开关状态:
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked && switch0.isChecked()) {
// Do something
}
else {
normal_main_list();
}
}
});
switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked && switch1.isChecked()) {
// Do something
else {
normal_main_list();
}
}
});
改变这个:
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggled_if_debug_activated();...
checker1 = true;
}
else {
if(checker1 && checker2){
normal_main_list();
}
}
}
});
switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggled_if_image_mode_activated();
checker2 = true;
}
else {
if(checker1 && checker2){
normal_main_list();
}
}
}
});
你可以这样做:
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggled_if_debug_activated();
checker1 = true;
}
else {
checker1 = false;
normal_main_list();
}
if (checker1 && checker2) {
// your code here
}
}
});
switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggled_if_image_mode_activated();
checker2 = true;
}
else {
checker2 = false
normal_main_list();
}
if (checker1 && checker2) {
// your code here
}
}
});
我有两个触发某些操作的开关按钮。如果我激活 A,动作 A 会发生,如果我激活 B,动作 B 会发生,但如果我同时激活 A 和 B,则什么也不会发生。这是我的代码:
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggled_if_debug_activated();
checker1 = true;
}
else {
normal_main_list();
}
}
});
switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggled_if_image_mode_activated();
checker2 = true;
}
else {
normal_main_list();
}
}
});
我尝试了很多方法,但没有任何效果。如何添加仅在两个开关都打开时才触发的动作 C(例如 toggled_if_all_checked)?据我研究 .setOnCheckedChangeListener 不支持任何类似 (switch0 && switch1).setOnCheckedChangeListener(...) 的东西。那么我该怎么做呢?
在此先感谢您,愿您的道路通向温暖的沙滩!
================================ 编辑 - 解决方案 ========== ==================
Liar 的解决方案几乎完美无缺,但我有一些错误,分别在激活两个开关并尝试仅停用其中一个时,输出有点错误。更好的解释:
You activate Switch A => Output A; You activate Switch B => Output B; You activate Switch A && B => Output C; You deactivate either A or B and the program was showing Outputs B or A (Basically upside down working)
这是我想出的代码:
switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked && switch1.isChecked())
{
toggle_C();
}
else
if(isChecked && !switch1.isChecked())
{
toggle_A();
}
else
if (!isChecked && switch1.isChecked())
{
toggle_B();
}
else {
toggle_DEFAULT();
}
}
});
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked && switch0.isChecked())
{
toggle_C();
}
else
if(isChecked && !switch0.isChecked())
{
toggle_B();
}
else
if (!isChecked && switch0.isChecked())
{
toggle_A();
}
else {
toggle_DEFAULT();
}
}
});
也许这不是最专业的解决方案,但它工作正常并且没有错误。感谢所有帮助过我的人!愿你的道路将你引向温暖的沙滩
您可以像这样检查另一个开关状态:
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked && switch0.isChecked()) {
// Do something
}
else {
normal_main_list();
}
}
});
switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked && switch1.isChecked()) {
// Do something
else {
normal_main_list();
}
}
});
改变这个:
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggled_if_debug_activated();...
checker1 = true;
}
else {
if(checker1 && checker2){
normal_main_list();
}
}
}
});
switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggled_if_image_mode_activated();
checker2 = true;
}
else {
if(checker1 && checker2){
normal_main_list();
}
}
}
});
你可以这样做:
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggled_if_debug_activated();
checker1 = true;
}
else {
checker1 = false;
normal_main_list();
}
if (checker1 && checker2) {
// your code here
}
}
});
switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggled_if_image_mode_activated();
checker2 = true;
}
else {
checker2 = false
normal_main_list();
}
if (checker1 && checker2) {
// your code here
}
}
});