2013-04-24 63 views
0

多個複選框的值,我有其中之一爲一週的每一天:獲取Android上

mondayRadioButton.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View view) { 
     if (mondayRadioButton.isChecked()){ 
      deleteAppointmentsLayout.removeAllViews(); 
      daySelected = 1; 
      for(Iterator<Appointment> i = appointments.iterator(); i.hasNext();){ 
       Appointment item = i.next(); 
        if(item.getDay() == 1){ 
         checkBox = new CheckBox(DeleteAppointmentActivity.this); 
         System.out.println("fucken did work"); 
         id = item.getId(); 
         time = item.getTime(); 
         duration = item.getDuration(); 
         description = item.getDescription(); 
         boxText = time + ", " + duration + ", " + description; 
         checkBox.setText(boxText); 
         checkBox.setTextSize(12); 
         checkBox.setId((int) id); 
         deleteAppointmentsLayout.addView(checkBox); 
        } 
        else { 
         System.out.println("fucken didnt work"); 
        } 
       } 
      } 
     } 
    }); 

當一個按鈕一個onclick被激活,我要檢索的信息對每個選定複選框當前選擇的日期(複選框以編程方式生成)。當刪除按鈕的onclick被激活時,如何檢查選擇了哪些選項?

+0

checkBox.isChecked() – Tom 2013-04-24 18:20:58

+0

你試試我的答案? – codeMagic 2013-04-24 23:03:58

回答

0

創建一個成員變量Array

public class MyClass extends Activity 
{ 
    ArrayList<CheckBox> cbArray = new ArrayList<CheckBox>(); 

那麼當你創建一個複選框將它添加到ArrayList。現在,當您點擊刪除Button使用for loop遍歷ArrayList和每一個來電isChecked()。 然後刪除或添加到一個檢查Array做任何你需要它

for (int i=0; i<cbArray.size(); i++) 
{ 
    if (cbArray.get(i).isChecked()) 
    { 
     // do whatever here