如何在 android studio 的复选框的 switch case 语句中使用 OR
how to use OR in switch case statement for checkbox in android studio
switch (view.getId()) {
case R.id.fruit_apple:
case R.id.fruit_orange:
if (checked) {
selection.add("Orange");
} else {
selection.remove("Orange");
}
break;
case R.id.fruit_grapes:
if (checked) {
selection.add("Grapes");
} else {
selection.remove("Grapes");
}
break;
}
如何防止上面代码输出的橙色值重复?当用户选中 apple 和 orange 时,它会显示两次 orange。
输出样本:
String apple,orange,grapes;
CheckBox check_apple, check_orange, check_grapes;
check_apple.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()){
apple = checkBox.getText().toString();
}
}
});
check_orange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()){
orange = checkBox.getText().toString();
}
}
});
check_grapes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()){
grapes = checkBox.getText().toString();
}
}
});
btn_done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Submit Data: ",apple+orange+grapes);
}
});
为避免列表中出现橙色重复项,您需要申请
if (checked) {
if(!selection.contains("Orange")
selection.add("Orange");
} else {
if(selection.contains("Orange")
selection.remove("Orange");
}
其他案例陈述也是如此。
试试下面的代码 -
switch (view.getId()) {
case R.id.fruit_apple:
if (checked) {
selection.add("Apple");
} else {
selection.remove("Apple");
}
break;
case R.id.fruit_orange:
if (checked) {
selection.add("Orange");
} else {
selection.remove("Orange");
}
break;
case R.id.fruit_grapes:
if (checked) {
selection.add("Grapes");
} else {
selection.remove("Grapes");
}
break;
}
switch (view.getId()) {
case R.id.fruit_apple:
case R.id.fruit_orange:
if (checked) {
selection.add("Orange");
} else {
selection.remove("Orange");
}
break;
case R.id.fruit_grapes:
if (checked) {
selection.add("Grapes");
} else {
selection.remove("Grapes");
}
break;
}
如何防止上面代码输出的橙色值重复?当用户选中 apple 和 orange 时,它会显示两次 orange。
输出样本:
String apple,orange,grapes;
CheckBox check_apple, check_orange, check_grapes;
check_apple.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()){
apple = checkBox.getText().toString();
}
}
});
check_orange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()){
orange = checkBox.getText().toString();
}
}
});
check_grapes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox)v;
if(checkBox.isChecked()){
grapes = checkBox.getText().toString();
}
}
});
btn_done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Submit Data: ",apple+orange+grapes);
}
});
为避免列表中出现橙色重复项,您需要申请
if (checked) {
if(!selection.contains("Orange")
selection.add("Orange");
} else {
if(selection.contains("Orange")
selection.remove("Orange");
}
其他案例陈述也是如此。
试试下面的代码 -
switch (view.getId()) {
case R.id.fruit_apple:
if (checked) {
selection.add("Apple");
} else {
selection.remove("Apple");
}
break;
case R.id.fruit_orange:
if (checked) {
selection.add("Orange");
} else {
selection.remove("Orange");
}
break;
case R.id.fruit_grapes:
if (checked) {
selection.add("Grapes");
} else {
selection.remove("Grapes");
}
break;
}