【问题标题】:i want to decrease count when already checked checkbox is unchecked. how do i do this?当已选中的复选框未选中时,我想减少计数。我该怎么做呢?
【发布时间】: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


    【解决方案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
        }
    
        }
    });
    

    等等……

    【讨论】:

    • 但这并不限制用户只检查 3 个复选框。这就是我想要做的。请帮助。谢谢。
    猜你喜欢
    • 2022-06-10
    • 2012-05-26
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多