虽然点击按钮我已经添加了一个复选框,最后需要通过点击提交按钮来获取所有选中的复选框值,
这是我的代码
mIncrementButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 0, 0);
CheckBox checkbox = new CheckBox(MainActivity.this);
checkbox.setLayoutParams(params);
mAllText.add(checkbox);
checkbox.setText("Checkbox" + j);
checkboxlayout.addView(checkbox);
j++;
Log.d("j", "--->" + j);
}
});
mGetTheVlue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("Inside Button", "Inside Burron" + mAllText.size());
for (int i = 0; i < mAllText.size(); i++) {
CheckBox check = mAllText.get(i);
if (check.isChecked()) {
AllCheckbox.add(check.getText().toString());
}
}
}
});
现在我需要检查是否所有复选框都被选中并且需要获取值, 不知道这个问题是否太老了。但我无法从谷歌获得任何解决方案,请帮助获得解决方案。
提前致谢。
最佳答案
此外,您不需要将所有 CheckBox 都存储在 Collection 中。您可以从布局中获取 subview 并检查它们是否为 CheckBox。
mGetTheVlue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("Inside Button", "Inside Burron" + checkboxlayout.getChildCount());
for(int i=0; i<checkboxlayout.getChildCount(); i++) {
View nextChild = checkboxlayout.getChildAt(i);
if(nextChild instanceOf CheckBox)
{
CheckBox check = (CheckBox) nextChild;
if (check.isChecked()) {
AllCheckbox.add(check.getText().toString());
}
}
}
}
});
关于android - 无法从动态添加的复选框中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22855276/