广播组未设置为单选按钮Android

时间:2017-07-28 04:53:23

标签: java android

我从数据库中获取数据。根据该值,我动态创建单选按钮。当我尝试将无线电组添加到该单选按钮时,无线电组未设置。 Bcoz,我可以一次选择许多单选按钮。

attr_layout[i].addView(radioButton, lp);
attr_layout[i]` is the value for buttons which im getting. The values 
are Small, Medium and Large. 

这是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);`         

3 个答案:

答案 0 :(得分:0)

无线电组只允许同时检查属于它的一个按钮(如果您正在尝试检查无线电组的所有按钮)。

答案 1 :(得分:0)

将radioButtons添加到广播组中,然后添加到布局中。

使用代码

radioGroup.addView(radioButton, lp);
attr_layout[i].addView(radioGroup)

而不是

attr_layout[i].addView(radioButton, lp);` 

答案 2 :(得分:0)

radioGroup.addView(radioButton, lp);代替attr_layout[i].addView(radioButton, lp);,然后在添加所有attr_layout[i].addView(radioGroup);后添加radioButton,即在for循环之外。