【发布时间】:2016-05-01 02:28:13
【问题描述】:
我试图制作一个仅选择 3 个症状的列表。 为此我放置了一个while循环,当计数变为3时停止。 选中复选框时计数会增加,但再次取消选中时计数不会减少。我该如何解决这个问题。这是我到目前为止所做的:-
JAVA 代码
public void onCheckboxClick (View view) {
int count = 0;
String a, b, c;
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
CheckBox checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
CheckBox checkBox4 = (CheckBox) findViewById(R.id.checkBox4);
CheckBox checkBox5 = (CheckBox) findViewById(R.id.checkBox5);
while(count<3){
switch (view.getId()) {
case R.id.checkBox1:
if (checkBox1.isChecked()) {
if (count == 0) {
a = "Bad taste in mouth";
} else if (count == 1) {
b = "Bad taste in mouth";
} else if (count == 2) {
c = "Bad taste in mouth";
}
count++;
} else {
}
break;
case R.id.checkBox2:
if (checkBox2.isChecked()) {
if (count == 0) {
a = "Gap in between teeth";
} else if (count == 1) {
b = "Gap in between teeth";
} else if (count == 2) {
c = "Gap in between teeth";
}
count++;
} else {
}
break;
case R.id.checkBox3:
if (checkBox3.isChecked()) {
if (count == 0) {
a = "Bad breath";
} else if (count == 1) {
b = "Bad breath";
} else if (count == 2) {
c = "Bad breath";
}
count++;
} else {
}
break;
case R.id.checkBox4:
if (checkBox4.isChecked()) {
if (count == 0) {
a = "Nasal pain";
} else if (count == 1) {
b = "Nasal pain";
} else if (count == 2) {
c = "Nasal pain";
}
count++;
} else {
}
break;
case R.id.checkBox5:
if (checkBox5.isChecked()) {
if (count == 0) {
a = "Blurred vision";
} else if (count == 1) {
b = "Blurred vision";
} else if (count == 2) {
c = "Blurred vision";
}
count++;
} else {
}
break;
}
}
}
XML 代码
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bad taste in mouth"
android:id="@+id/checkBox1"
android:onClick="onCheckboxClick"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gap in between teeth"
android:id="@+id/checkBox2"
android:onClick="onCheckboxClick"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bad breath"
android:id="@+id/checkBox3"
android:onClick="onCheckboxClick"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nasal pain"
android:id="@+id/checkBox4"
android:onClick="onCheckboxClick"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blurred vision"
android:id="@+id/checkBox5"
android:onClick="onCheckboxClick"/>
任何帮助将不胜感激谢谢:)
【问题讨论】:
标签: android xml loops checkbox count