Android 如何将单选按钮添加到单选组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10565989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add radio buttons to radio group
提问by ΧρησΤ?κη? Τσαν
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:
我有一个 TableLayout 并且在每行的第三列中我想放置一个单选组。我像这样构建 RadioButtons:
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?
但是这会导致我的应用程序崩溃,有什么想法吗?
回答by Sam
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.
您正在构建一个长度为 的原始数组megethos
,但您的循环使用长度size
。如果megethos
和size
是不同的值,这可能会导致许多不同类型的错误......但是所有这些都是多余的,因为 RadioGroup 会为您保持这个数组的最新状态。
I would try something like this:
我会尝试这样的事情:
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);
}
And when you want to reference a button at index
:
当你想引用一个按钮时index
:
group.getChildAt(index);
Also please always post your logcat errors, it tells us exactlywhat went wrong and where to look. Otherwise we have to guess like this.
另外,请始终发布您的 logcat 错误,它会告诉我们究竟出了什么问题以及在哪里查看。否则我们就得这样猜了。
Update
更新
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]);
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:
一个视图只能有一个父级。据我所知,如果不先进行大量自定义,您就无法将 RadioGroup 拆分为多个视图。然而,ListView 已经具有内置功能 setChoiceMode(),其行为类似于 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);
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.)
您可以轻松适应simple_list_item_checked
显示 SSID 和信号强度。希望有帮助。(如果您等待足够长的时间 imran khan 可能会用图形更改剪切并粘贴我的答案,然后再次将其声明为他自己的。)
回答by Yogesh Srivastava
I have created one demo app in which I have added Radio buttons dynamically and also handled click events.
我创建了一个演示应用程序,在其中动态添加了单选按钮并处理了单击事件。
public class MainActivity extends AppCompatActivity {
公共类 MainActivity 扩展 AppCompatActivity {
RadioGroup radioGroup2;
ArrayList<String> buttonNames;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
buttonNames = new ArrayList<>();
buttonNames.add("No Tip");
buttonNames.add("10%");
buttonNames.add("20%");
buttonNames.add("30%");
radioGroup2.setWeightSum(Float.parseFloat(buttonNames.size() + ""));
radioGroup2.setBackgroundColor(Color.BLUE);
for (int i = 0; i < buttonNames.size(); ++i) {
RadioButton radioButton = new RadioButton(this);
radioButton.setId(i);
RadioGroup.LayoutParams childParam1 = new RadioGroup.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f);
childParam1.setMarginEnd(2);
radioButton.setGravity(Gravity.CENTER);
radioButton.setLayoutParams(childParam1);
radioButton.setBackground(null);
radioButton.setText(buttonNames.get(i));
radioButton.setTextColor(Color.BLUE);
radioButton.setBackgroundColor(Color.WHITE);
radioButton.setButtonDrawable(null);
if (buttonNames.get(i).equals("20%")) {
radioButton.setChecked(true);
radioButton.setBackgroundColor(Color.BLUE);
radioButton.setTextColor(Color.WHITE);
}
radioGroup2.addView(radioButton, childParam1);
}
radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i1) {
RadioButton button = null;
for (int i = 0; i < buttonNames.size(); ++i) {
if (i1 == i) {
button = (RadioButton) findViewById(i1);
button.setChecked(true);
button.setBackgroundColor(Color.BLUE);
button.setTextColor(Color.WHITE);
Toast.makeText(getApplicationContext(), button.getText() + " checked", Toast.LENGTH_SHORT).show();
} else {
button = (RadioButton) findViewById(i);
button.setChecked(false);
button.setBackgroundColor(Color.WHITE);
button.setTextColor(Color.BLUE);
}
}
}
});
}
}
}