答案 0 :(得分:1)
将您的第一个for循环更改为此
for (int i = 0; i < items.size(); i++) {
CheckBox cb = new CheckBox(this);
cb.setText(String.valueOf(items.get(i)));
cb.setId(items.get(i));
ll.addView(cb);
}
编辑:要在单个Toast中显示所有值,您可以执行类似
的操作@Override
public void onClick(View v) {
String message = "";
for (int i : items) {
//boolean checked = ((CheckBox)v).isChecked();
CheckBox ch=(CheckBox) findViewById(i);
if (ch.isChecked()) {
message = message + " " + ch.getText() + " " ;
}
}
Toast.makeText(getApplicationContext(),
"Button is clicked " + message, Toast.LENGTH_LONG).show();
}
答案 1 :(得分:1)
尝试将复选框的ID设置为items.get(i)not i