2012-02-22 199 views
0

我試圖獲取有多少個複選框被選中的計數,如果您取消選擇一個,則假設將計數器更新爲-1 ......所以如果我選擇4檢查盒子,它說「檢查少」=計數4 ...然後我選擇3和計數器= 3..This是我有: // ---複選框1 --- 最後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 2012-02-22 06:29:15

回答

2

我認爲你應該這樣做,而不是:

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

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

感謝它的工作。 – user1179083 2012-02-22 05:50:24

+0

@Maurice如果我的'ListView'中的CheckBox數字是動態的,我怎麼做這個。我如何知道在哪裏檢查'checkBoxNumber.isChecked()'? – 2013-12-04 02:53:45