2017-07-27 98 views
0

我想让用户从许多单选按钮中只选择一个单选按钮。现在,我可以选择许多单选按钮。允许用户只从众多选项中选择一个单选按钮

这里是我的代码

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

} 

我怎样才能做到这一点?

+0

使用无线电集团的https:// WWW。 mkyong.com/android/android-radio-buttons-example/ –

+0

使用RadioGroup。 –

+0

RadioButton应该是RadioGroup的直接子代 – sForSujit

回答

1

在XML文件中添加此:

<RadioGroup 
    android:id="@+id/radiogroup" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" /> 

并执行以下操作:

RadioGroup rgp = (RadioGroup) findViewById(R.id.radiogroup); 
RadioGroup.LayoutParams rprms; 

for(int i = 0; i < 3; i++) { 
    RadioButton radioButton = new RadioButton(this); 
    radioButton.setText("new" + i); 
    radioButton.setId("rbtn" + i); 
    rprms = new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    rgp.addView(radioButton, rprms); 
} 
+0

我在这里有属性[我]。 attr_layout [i] .addView(radioButton,params); – Jacky

+0

我知道我应该让它成为广播组的名字。但是,如果我在这里给出这样的结果,我得到错误 – Jacky

+0

对不起,我没有得到你。 –

-1

使用RadioGroup这样的:

<RadioGroup 
     android:id="@+id/radioGroup" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <RadioButton 
      android:id="@+id/radio1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/radio_male" 
      android:checked="true" /> 

     <RadioButton 
      android:id="@+id/radio2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/radio_female" /> 

    </RadioGroup> 
+2

他正在通过代码动态添加按钮。不知道这将如何帮助他 –

+0

我想通过编程实现它。你可以检查代码并进行修改吗? – Jacky

+0

@kapsym是的。我正确 – Jacky

相关问题