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

2023-09-08 08:37:23 作者:共江湖同醉

我需要动态添加单选按钮,装置单选按钮可以是3,4,5或6,这将是水平增补而一行包含最多3个单选按钮,如果有3个以上的话,那就来的下面上面的单选按钮的排在网格视图,我stukking这里2天,但没有得到还没有任何解决方案..please帮助,任何帮助将是pciated高度AP $ P $ .. 我的code的单选按钮均低于但它显示在一行中的所有单选按钮,..意味着它的隐藏单选按钮。

main.xml中:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
 机器人:方向=垂直>

   <的TextView
 机器人:ID =@ + ID / TextView01
  机器人:layout_width =WRAP_CONTENT
  机器人:layout_height =WRAP_CONTENT
  机器人:文本=选择您最喜爱的女主角>
  < / TextView的>

 < RadioGroup中
机器人:ID =@ + ID / RadioGroup01
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
 < / RadioGroup中>
 <按钮
机器人:ID =@ + ID / Button01
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=提交>
 < /按钮>
< / LinearLayout中>
 

和java类是:

  @覆盖
  公共无效的onCreate(包savedInstanceState){
       super.onCreate(savedInstanceState);
       的setContentView(R.layout.main);

       DisplayRadioButton();
  }


  公共无效DisplayRadioButton(){
       的for(int i = 0;我小于10;我++){
           RadioGroup中RadioGroup中=(RadioGroup中)findViewById(R.id.RadioGroup01);
           单选rdbtn =新的单选按钮(这一点);
           rdbtn.setId(ⅰ);
           rdbtn.setText(文[I]);
           radiogroup.addView(rdbtn);
  }

}
 

解决方案 累活脏活全靠它 科沃斯机器人曝光

请尝试以下方式:

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>