我有动态创建的单选按钮。
LinearLayout linLayRoot = (LinearLayout)dialogView.findViewById(R.id.dialog_layout_root);
RadioGroup radGp = new RadioGroup(this);
linLayRoot.addView(radGp);
for (String dir : dirArray)
{
LinearLayout linLayNew = new LinearLayout(this);
linLayNew.setGravity(0x10);
RadioButton radBut = new RadioButton(this); /// <- this button does not work!
radBut.setText("");
TextView tv = new TextView(this);
tv.setText(dir);
tv.setPadding(10, 0, 20, 0);
ImageView ivs = new ImageView(this);
linLayNew.addView(radBut);
linLayNew.addView(tv);
linLayNew.addView(ivs);
radGp.addView(linLayNew);
}
RadioButton radBut1 = new RadioButton(this); /// <- this button works!
radBut1.setId(11);
radBut1.setText("a1");
radGp.addView(radBut1);
RadioButton radBut2 = new RadioButton(this); /// <- this button works!
radBut2.setId(12);
radBut2.setText("b2");
radGp.addView(radBut2);
radGp.setOnCheckedChangeListener( new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Toast.makeText(getApplicationContext(), String.valueOf(checkedId) , Toast.LENGTH_SHORT).show();
}
});
但正如你从上面的评论中看到的,它们并不真的起作用,也就是说,它们似乎没有绑定到radgp……也许是因为他们是在一个单独的林利布局?
谢谢!
最佳答案
将RadioButtons
添加到List<RadioButton>
中,然后使用
mRadioList.get(i).setChecked(true);