本文介绍了如何设置动态创建的单选按钮变成RadioGroup中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个动态创建单选按钮。
I have Radio buttons that are created dynamically.
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 ......也许它,因为他们是在一个单独的linlearlayout?
But as you can see from the comments above, they don't really work, i.e. seems that they aren't binded to the radGp... maybe its because they are in a separate linlearlayout?
谢谢!
推荐答案
添加您的单选按钮
在列表&LT;单选&GT;
键,您可以检查它们然后使用
Add your RadioButtons
in a List<RadioButton>
and you can check them then by using
mRadioList.get(i).setChecked(true);
这篇关于如何设置动态创建的单选按钮变成RadioGroup中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!