我希望在已选中复选框时减少计数复选框未选中。我该怎么做呢?

时间:2016-04-30 18:22:27

标签: android xml loops checkbox count

我试图制作一个只会选择3种症状的清单。 我把一个while循环当计数变为3时停止。 选中复选框时计数会增加,但再次取消选中时,计数不会减少。我该如何解决这个问题。这是我到目前为止所做的: -

JAVA CODE

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 CODE

    <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"/>

任何帮助将不胜感激,谢谢:)

1 个答案:

答案 0 :(得分:1)

不是使用while循环,而是为每个复选框使用OnCheckedChangeListener。

checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked ) {
            count++;
        } else {
            count--;
        }

    if (count >= 3) {
    // do logic
    }

    }
});

等...