2013-11-23 77 views
0

我已經在表格佈局中動態創建了複選框。我需要我的第一個複選框應該在開始時啓用,其餘的應該不啓用,然後如果我點擊我的第一個複選框第二個CB應該啓用,然後等等。動態複選框android

表格創建複選框。

TableLayout ll = (TableLayout) findViewById(R.id.tablelayout22); 


final TableRow tr = new TableRow(this);      
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 

tr.setId(i);     
tr.setLayoutParams(lp); 
lp.setMargins(10, -10, 15, 5); 

CheckBox feature1=new CheckBox(this); 
feature1 = new CheckBox(this); 
feature1.setId(i); 

final TextView fin = new TextView(this); 
fin.setLayoutParams(lp); 
fin.setText(FinancialYear1); 

tr.addView(feature1); 
tr.addView(fin); 

ll.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

OncheckChangedListener

feature1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     @Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    if (isChecked) { 
     //...... 
    }        
    else{ 


    //...... 
    } 

回答

0

,如果你想設置第一CB爲true,則必須添加如下鱈魚:

boolean b = true; 
feature1.setEnabled(b) 

並與b值做你想做的做,

在繼續,如果你第一次在OncheckChangedListener檢查,你必須得到CB的ID,並與所有你想做的事改變對方CB

UPDATE /////

你把所有的ID,所以你必須保持它在一些列表或陣列,當用戶打一個CB for語句你想要的每一件事情,我寫半碼爲您提供:

for (int i = 0 ; i < list.size ; i++) 
{ 
    if (!list.get(i).equal (id)) 
    { 
    Checkbox ch = findViewByid(list.get(i); 
    /// do what you want to other checkbox 
    } 
    else 
    /// do what you want with checked CB 
} 

抱歉,因爲我不靠近任何IDE來測試我的代碼,抱歉代碼錯誤,我希望這可以幫助你

+0

我的第一個CB應該是ENA開始放血,沒有檢查。 – Yashwanth

+0

關於啓用您的意思是什麼? –

+0

看到我的edit.is這你想要嗎? –