2017-07-28 40 views
0

我正在从数据库中提取数据。根据这个值,我动态创建单选按钮。当我尝试将收音机组添加到该单选按钮时,收音机组未设置。我可以一次选择许多单选按钮。无线电组未设置为单选按钮Android

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

回答

0

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

使用代码

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

代替

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

让我试试这一个 – Jacky

+0

与上述代码尝试。现在,它允许我选择两个按钮。我有3个单选按钮。我可以选择2个单选按钮。为什么它那样? – Jacky

+0

你可以检查图像吗? http://imgur.com/a/HBWwr – Jacky

0

广播组允许在检查一次只有一个按钮属于它(如果你想检查单选按钮组的所有按钮)。

0

radioGroup.addView(radioButton, lp);,而不是attr_layout[i].addView(radioButton, lp);,然后将所有radioButton S,即for循环外后放attr_layout[i].addView(radioGroup);

+0

我做了你在这里提到的。现在,我可以选择三个中的两个。需要使其仅选择一个。 – Jacky

+0

检查是否在同一个RadioGroup中放置了所有您想要互相排斥的按钮。您可能在不同的组内有按钮,可以同时选择。 – Nithin

+0

是的,我确实..你可以在这里查看完整的代码。 https://pastebin.com/vVQ87MkR – Jacky

相关问题