[英]Android - How to get checked state of CheckBoxes in dynamically added LinearLayouts?
我想知道,我有一個垂直LinearLayout,可以動態添加水平LinearLayouts。 每個水平LinearLayout都包含一個CheckBox。 我想知道,如何在每個動態添加的LinearLayout中檢索每個CheckBox的選中狀態?
for (int i = 0; i < array.size(); i++) {
LayoutInflater layoutInflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout testView = (LinearLayout) layoutInflater.inflate(R.layout.balance_test_layout, null);
for (int j = 0; j < customConditions.size(); j++) {
TextView checkView = new TextView(BalanceActivity.this);
checkView.setPadding(padding, padding, padding, padding);
checkView.setTextSize(20);
checkView.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams checkTextParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 45, getResources().getDisplayMetrics())));
checkTextParams.gravity = Gravity.CENTER;
checkView.setLayoutParams(checkTextParams);
testView.addView(checkView);
}
CheckBox option = (CheckBox) testView.findViewById(R.id.testCheckBox);
option.setId(i);
option.setText(array.get(i));
//code for of rest of LinearLayout
}
okButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//need to get values of CheckBoxes in here
}
});
我想到了:
okButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < testDialogLayout.getChildCount(); i++) {
View innerView = testDialogLayout.getChildAt(i);
if (innerView instanceof LinearLayout) {
LinearLayout linearLayout = (LinearLayout) innerView;
CheckBox cb = (CheckBox) linearLayout.findViewById(i);
//do something with cb
}
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.