我试图计算选择了多少个复选框的计数,如果你取消选择一个,则假设将计数器更新为-1 ......所以,如果我选择4个复选框,它会显示“检查少“=计数4 ...然后我选择3,计数器= 3 ..这是我有的: // --- CheckBox 1 --- final CheckBox checkBox =(CheckBox)findViewById(R.id.checkBox1);
// ---CheckBox 2---
final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
// ---CheckBox 3---
final CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
// ---CheckBox 4---
final CheckBox checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (checkBox.isChecked())
count++;
if (checkBox.isPressed())
count--;
if (checkBox2.isChecked())
count++;
if (checkBox2.isPressed())
count--;
if (checkBox3.isChecked())
count++;
if (checkBox3.isPressed())
count--;
if (checkBox4.isChecked())
count++;
if (checkBox4.isPressed())
count--;
if (count == pubsSelected){
Intent myIntent = new Intent(view.getContext(), Screen2.class);
startActivityForResult(myIntent, 0);
}
else if (count < pubsSelected)
DisplayToast("Pick more pubs/holes");
else if (count > pubsSelected)
DisplayToast("Pick less pubs/holes");
}
});
}
答案 0 :(得分:2)
我认为你应该这样做:
if (checkBox.isChecked())
{
count++;
}else
{
count--;
}
if (checkBox2.isChecked())
{
count++;
}else
{
count--;
}