【发布时间】:2014-12-23 12:43:11
【问题描述】:
我正在创建 RadioButtons 并将它们动态添加到 RadioGroup。 但是当我运行应用程序时,RadioButtons 的文本没有显示在屏幕上。
这是我的 RadioButtons 代码
else if ((items.get(i).toString()).equals("rad")) {
RadioGroup bg = new RadioGroup(getApplicationContext());
int child=0;
for (int h = textlen; h < text.size(); textlen++) {
if (text.get(textlen).contains("(")) {
s = text.get(textlen).replace("(", "");
if (s.contains(")"))
s = s.replace(")", "");
} else if (text.get(textlen).contains(")")) {
s = text.get(textlen).replace(")", "");
} else
s = text.get(textlen);
RadioButton radioButton = new RadioButton(
getApplicationContext());
bg.addView(radioButton);
// / radioButton.setName("rbt");
if (s.contains("{on}")) {
// radioButton.setSelected(true);
radioButton.setChecked(true);
s = s.replace("{on}", "");
} else {
radioButton.setChecked(false);
s = s.replace("{of}", "");
}
//((RadioButton)bg.getChildAt(child)).setText(s);
//child++;
radioButton.setText(s);
String c = text.get(textlen).substring(
text.get(textlen).length() - 1);
if (c.equals(")")) {
textlen++;
break;
}
}
layout.addView(bg);
}
当我调试代码时,我可以看到文本已添加到 RadioGropus 子项中
但在运行应用程序时,文本是不可见的。 任何人都可以发现这个问题。 非常感谢
【问题讨论】:
-
尝试先设置文本然后使用
bg.addView(radioButton);也可以使用setId()为单选按钮分配一个id
标签: android radio-button android-radiogroup