简体   繁体   中英

dynamic Check box android

I've created check box dynamically in a table layout. I need my first check box should be enabled at the start and rest of them should not be enabled, then if i click my first check box second CB should be enabled then so on..

Table creation with checkbox.

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{


    //......
    }

if you want set first CB to true you must add follow cod:

boolean b = true;
feature1.setEnabled(b)

and with value of b do all you want to do,

and in continue if first you checked in OncheckChangedListener you must get id of CB and change the other CB with all you want to do

UPDATE /////

you have all id, so you must keep it in some list or array, and when the user hit one CB in for statement do every thing you want, i write semi code for you :

 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
 }

sorry because i'm not near any IDE to test my code, sorry for code error, i hope this can help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM