2012-02-22 12 views
0

いくつのチェックボックスが選択されているかを調べようとしていますが、選択を解除するとカウンタが-1に更新されていると思います。// --- CheckBox 1 --- final CheckBox checkBox =(CheckBox)このチェックボックスをオンにすると、チェックボックスがオフになります。 findViewById(R.id.checkBox1);チェックボックス状態android eclipse

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

代わりにチェックボックスに別の変数を使用しての、あなたがチェックボックスの配列を持つことができます。 – noob

答えて

2

私はあなたの代わりにこれを行うべきだと思う:

if (checkBox.isChecked()) 
{ 
    count++; 
}else 
{ 
    count--; 
} 

if (checkBox2.isChecked()) 
{ 
    count++; 
}else 
{ 
    count--; 
} 
+0

ありがとうございます。 – user1179083

+0

@Maurice私の 'ListView'の' CheckBox'番号が動的なので不明な場合、どうすればいいですか? 'checkBoxNumber.isChecked()'のチェックをどこで行うべきかをどのように知っていますか? –

関連する問題