如何单选按钮添加到无线电集团 [英] How to add radio buttons to radio group
问题描述
我有一个TableLayout,并在每行的第三列我要放置一个单选按钮组。
我打造的单选按钮是这样的:
I have a TableLayout and in the third column of every row I want to place a radio group. I build the RadioButtons like this:
rg = (RadioGroup) findViewById(R.id.radioGroup1);
for (int k = 0; k < size; k++) {
rb[k] = new RadioButton(context);
rg.addView(rb[k]);
}
然而,这引起我的应用程序崩溃,任何想法?
However this cause my app to crash, any ideas?
推荐答案
您正在与 megethos
的长度基本数组,但你的循环使用长度尺寸
。如果 megethos
和尺寸
是不同的值,这可能会导致许多不同类型的错误......但所有这些多余的,因为一个RadioGroup中保持这个数组是最新的你。
You are building a primitive array with the length of megethos
, but your loop uses the length size
. If megethos
and size
are different values this can cause many different types of errors... But all of this redundant since a RadioGroup keeps this array up to date for you.
我会尝试这样的事:
RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton button;
for(int i = 0; i < 3; i++) {
button = new RadioButton(this);
button.setText("Button " + i);
group.addView(button);
}
当你想在索引引用一个按钮
:
group.getChildAt(index);
另外,请随时发表您的logcat的错误,它告诉我们的究竟的什么地方出了错,并去哪里找。否则,我们不得不猜是这样的。
Also please always post your logcat errors, it tells us exactly what went wrong and where to look. Otherwise we have to guess like this.
更新
这个错误是因为你试图在同一按钮添加到两个不同的布局:
The error is because you are trying to add the same button to two different layouts:
tr[k].addView(rb[k]);
rg.addView(rb[k]);
视图只能有一个父。据我知道你不能先破RadioGroup中分割成多个视图没有大量的定制。然而一个ListView已经有一个行为像一个RadioGroup中内置的功能setChoiceMode():
a view can only have one parent. As far as I know you cannot break a RadioGroup apart into multiple views without a lot of customization first. However a ListView already has the built-in feature setChoiceMode() that behaves like a RadioGroup:
List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, list);
ListView listView = (ListView) findViewById(R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setAdapter(adapter);
您可以轻松地适应 simple_list_item_checked
显示SSID和信号强度。希望有所帮助。 (如果你等待足够长的伊姆兰·罕可能剪切和放大器;粘贴我的答案与图形变化,然后声称它作为自己的一次。)
You can easily adapt simple_list_item_checked
to display the SSID and signal strength. Hope that helps. (If you wait long enough imran khan might cut & paste my answer with graphical change, then claim it as his own again.)
这篇关于如何单选按钮添加到无线电集团的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!