无论是否选中,复选框都会显示相同的结果

时间:2017-10-09 12:44:40

标签: java checkbox

我在使用Java中的复选框时遇到问题。无论是否选中该复选框,结果始终为“是”。有人可以帮我检查下面的代码是否有错误吗?

private String whippedCreamMethod(){
CheckBox checkBoxWhippedCream = (CheckBox) findViewById(R.id.whipped_cream_checkbox);
   if (checkBoxWhippedCream.isSelected()){
       whippedCream = "No";
   } else {
       whippedCream = "Yes";
   }
    return whippedCream;

}

1 个答案:

答案 0 :(得分:0)

我认为有一种方法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

相关问题