2016-07-13 115 views
1

我进行动态复选框,在我的项目中使用下面的代码,我怎么能得到复选框的ID并作出命令它例如:如何获取动态复选框ID?

if(checkbox1.ischecked()==true){ 
//do 
} 

预先感谢您。

public void fetchContacts() { 
    final LinearLayout Vll=(LinearLayout)findViewById(R.id.Vll); 

    CheckBox cb = new CheckBox(getApplicationContext()); 
         cb.setText(outputName+":"+outputNumber); 
         Vll.addView(cb); 
} 

回答

0

你应该存储在ArrayList

List<CheckBox > allCheckBox = new ArrayList<CheckBox>(); 

而当你所有的CheckBox所有引用创建任何CheckBox,你需要把它添加到您的List

CheckBox cb = new CheckBox(getApplicationContext()); 
... 
allCheckBox.add(cb); 

之后,你可以访问到CheckBox通过

allCheckBox.get(position).isChecked() == true 
// allCheckBox.get(position) -> with return a CheckBox 
// position is the index when you add checkbox to ArrayList, for example you have create 2 CheckBox dynamic 
// then CheckBox1 is allCheckBox.get(0) and CheckBox2 is allCheckBox.get(1) 
+0

错误发生时,我想用 – mrreza

+0

allCheckBox.get(位置).isChecked( )== true – mrreza

+0

在哪里我必须把上面的鳕鱼? – mrreza