2016-04-30 10 views
0

私は3つの症状だけを選択するリストを作ろうとしていました。 は、カウントが3になると停止するwhileループを入れます。 チェックボックスをオンにするとカウントが増加しますが、チェックを外すとカウントは減少しません。どのように私はこれを解決するのですか?ここで私はこれまでやっていることである: -既にチェックされている場合、カウントを減らしたいと思います。チェックボックスはオフになっています。どうすればいい?

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

任意の助けが理解されるであろうありがとう:)

答えて

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 
    } 

    } 
}); 

等...

+0

が、それは唯一の3のチェックボックスをチェックすることからユーザーを制限するものではありません。それが私がやろうとしていることです。 PLZ hlp。 thnx。 –

+0

4番目のチェックボックスがオンになったら、手動でチェックを外します –

関連する問題