我想让用户从许多单选按钮中只选择一个单选按钮。现在,我可以选择许多单选按钮。允许用户只从众多选项中选择一个单选按钮
这里是我的代码
RadioGroup radioGroup = new RadioGroup(mMain);
for (int k = 0; k < attr_size; k++) {
String price = String.format(Locale.ENGLISH, AppConstants.DECIMAL_POINTS, Float.parseFloat(attributes.get(k).getAttr_price()));
String name_price = attributes.get(k).getAttr_name()
+ " (" + mMain.getString(R.string.currency_code)
+ " " + price + ")";
if (!multiSelect.equals("1")) {
RadioButton radioButton = new RadioButton(mMain);
radioButton.setText(name_price);
radioButton.setId(i + 6);
radioButton.setTextSize(12);
radioButton.setTag(attributes.get(k));
radioButton.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
radioButton.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START);
}
setTextFont(radioButton, "Museo_Slab.otf");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
1f);
lp.setMargins(10, 10, 0, 10); // llp.setMargins(left, top, right, bottom);
radioButton.setLayoutParams(lp);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
attr_layout[i].addView(radioButton, lp);
}
}
我怎样才能做到这一点?
使用无线电集团的https:// WWW。 mkyong.com/android/android-radio-buttons-example/ –
使用RadioGroup。 –
RadioButton应该是RadioGroup的直接子代 – sForSujit