java如何在动态添加复选框时查看选中的复选框
我有一个包含TextView
和CheckBox
的XML,现在我将它们添加到活动中,
我添加了多少取决于有多少问题,但如果选中了复选框,我想看看是哪一个,我该怎么做
for (int i = 0; i< questions.size(); i++) {
View view1 = View.inflate(Checklist.this, R.layout.question, null);
TextView myText = (TextView) view1.findViewById(R.id.questiontext);
checkbox = (CheckBox) view1.findViewById(R.id.checkBox);
layout.addView(view1);
myText.setText(questions.get(i).question);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//check which checbox is checked
}
});
}
这就是如何将它们添加到我的视图中
# 1 楼答案
您可以使用
setTag
在复选框上设置标记:创建时:
这将给你标签(i),它将指示问题编号
希望有帮助