I am fetching data from database. Based on the value, I'm dynamically creating radio buttons. When I try to add radio group to that radio buttons, radio group is not setting up. Bcoz of that I can select many radio buttons at once.
attr_layout[i].addView(radioButton, lp);
attr_layout[i]` is the value for buttons which im getting. The values
are Small, Medium and Large.
Here is the complete code for RadioButton.
RadioGroup radioGroup = new RadioGroup(mMain);
LinearLayout[] attr_layout = new LinearLayout[optionsList.size()];
attr_layout[i] = new LinearLayout(mMain);
attr_layout[i].setOrientation(LinearLayout.HORIZONTAL);
int attr_size = attributes.size();
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")) // This multiselect value 1 and 0 is coming from database.
//Based on these value, app will display checkbox or radio button
{
final 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);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
radioGroup.setLayoutParams(params);
attr_layout[i].addView(radioButton, lp);`
单选组仅允许一次检查属于它的一个按钮(如果要检查单选组的所有按钮)。
Add radioButtons into radio group then add into layouts.
Use the code
radioGroup.addView(radioButton, lp);
attr_layout[i].addView(radioGroup)
instead of
attr_layout[i].addView(radioButton, lp);`
radioGroup.addView(radioButton, lp);
instead of attr_layout[i].addView(radioButton, lp);
and then put attr_layout[i].addView(radioGroup);
after adding all radioButton
s, ie outside the for
loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.