且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态添加单选按钮:机器人

更新时间:2023-11-19 08:05:19

请尝试以下方式:

1)在XML中删除 RadioGroup中。通过动态地创建

  RadioGroup中RadioGroup中[];
单选rdbtn [];
的LinearLayout线性[];

RadioGroup中=新RadioGroup中[9/3]
rdbtn =新单选[9];
线性=新的LinearLayout [9/3]

......
诠释计数= 0; //整数标志

的for(int i = 0; I< 9;我++){

  如果i的值等于3多个然后加1计数
         // SETT线性[计]的方向是水平的。

         root_layout.addView(线性[计数]);
         RadioGroup中[统计] =新RadioGroup中(本);
         线性[计数] .addView(RadioGroup中[计数]); //添加单选组线性布局

       添加单选按钮,单选组。

          rdbtn [我] =新的单选按钮(这一点);
          rdbtn [I] .addView(RadioGroup中[计数]);


}
 

我希望你能得到解决。要知道数组索引出界的异常。

您的XML可能看起来像:

 <的LinearLayout
           机器人:ID = rootlayout
           ..... //孩子的LinearLayout
                     ..。单选按钮组
                        ... 单选按钮
 < / LinearLayout中>
 

i need to add radio button Dynamically , means radio button may be 3 , 4, 5 or 6 and it would be Added Horizontally and one row contains maximum 3 radio button , if there are more then 3 then it would come below of above row of radio button as in grid view , i am stukking here from 2 days but not get yet any solution ..please help , any help would be highly appreciated.. my code for radio button are below but it display all radio button in a single row,..means its hiding the radiobutton..

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
 android:orientation="vertical" >

   <TextView
 android:id="@+id/TextView01"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Choose Your Favorite Actress" >
  </TextView>

 <RadioGroup
android:id="@+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
 </RadioGroup>
 <Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" >
 </Button>
</LinearLayout>

ANd java class is:

  @Override
  public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

       DisplayRadioButton();
  }


  public void DisplayRadioButton() {
       for(int i=0;i<10;i++) {
           RadioGroup radiogroup = (RadioGroup)findViewById(R.id.RadioGroup01);
           RadioButton rdbtn = new RadioButton(this);
           rdbtn.setId(i);
           rdbtn.setText(text[i]);
           radiogroup.addView(rdbtn);
  }

}

please try in the following way:

1)in your xml remove the RadioGroup. create it by dynamically

RadioGroup radiogroup[];
RadioButton rdbtn[];
LinearLayout linear[]; 

radiogroup = new RadioGroup[9/3];
rdbtn = new RadioButton[9];
linear = new LinearLayout[9/3];

......
int count = 0; // integer flag

for(int i=0;i<9;i++){

  if the value of i is equal to 3 multiple then increase count by 1
         // sett linear[count]'s orientation is horizontal.

         root_layout.addView(linear[count]);
         radiogroup[count] = new RadioGroup(this);
         linear[count].addView(radiogroup[count]);  // add radio group to linear layout

       add radio button to radio group.

          rdbtn[i] = new RadioButton(this);
          rdbtn[i].addView(radiogroup[count]);


}

i hope you get solved. be aware of array index out of bound exception.

your xml may look like:

<LinearLayout
           android:id= rootlayout
           ..... // the child linearlayout
                     .. . radio group
                        ... radio button
 </LinearLayout>