我在 Java 中使用复选框时遇到问题。无论是否选中该复选框,结果始终为"是"。有人可以帮我检查下面的代码是否有任何错误吗?
private String whippedCreamMethod(){
CheckBox checkBoxWhippedCream = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
if (checkBoxWhippedCream.isSelected()){
whippedCream = "No";
} else {
whippedCream = "Yes";
}
return whippedCream;
}
我认为有一种方法isChecked()
而不是isSelected()
;
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
if (checkBox.isChecked()) {
checkBox.setChecked(false);
}
与 https://developer.android.com/reference/android/widget/CheckBox.html 相比