Android 使用字符串数组动态创建RadioButton

Android 使用字符串数组动态创建RadioButton,android,Android,我正在尝试从字符串数组创建RadioButton,但如果我只使用字符串,它就不起作用了,那么它似乎可以正常工作 Log Cat显示错误“ava.lang.IllegalStateException:指定的子项已具有父项。必须首先对子项的父项调用removeView()。 我知道错误即将发生,因为RadioButton对象并没有动态创建,所以错误正在显示 我从这里获取代码以供参考,请检查此url 请检查代码并更正我,以便我进一步使用我的应用程序 这是代码 LinearLayout layout

我正在尝试从字符串数组创建RadioButton,但如果我只使用字符串,它就不起作用了,那么它似乎可以正常工作 Log Cat显示错误“ava.lang.IllegalStateException:指定的子项已具有父项。必须首先对子项的父项调用removeView()。

我知道错误即将发生,因为RadioButton对象并没有动态创建,所以错误正在显示

我从这里获取代码以供参考,请检查此url

请检查代码并更正我,以便我进一步使用我的应用程序 这是代码

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
RadioGroup radioGroup = new RadioGroup(this);

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );

        // adding Radio Group
        layout.addView(radioGroup, p);

        // creating Radio buttons Object//
        RadioButton radioButtonView = new RadioButton(this);
        String[] ab ={"1","2"}; 

        for(int i =0; i<ab.length;i++)
        {
            radioButtonView.setText(ab[i]);
            radioGroup.addView(radioButtonView, p);
            ((ViewGroup)layout.getParent()).removeView(layout);
        }
寻求帮助 谢谢

IllegalStateException:指定的子级已具有父级。你 必须首先对子级的父级调用removeView()

因为您正在
LinearLayout
中再次添加相同的
RadioButton
实例

在for循环中创建单选按钮的对象:

   for(int i =0; i<ab.length;i++)
    {
        RadioButton radioButtonView = new RadioButton(this);
        radioButtonView.setText(ab[i]);
        radioGroup.addView(radioButtonView, p);
        ((ViewGroup)layout.getParent()).removeView(layout);
    }   

for(int i=0;i错误在于您试图在同一布局中多次添加同一个单选按钮,您必须在for循环内移动创建的单选按钮

    String[] ab ={"1","2"}; 

    for(int i =0; i<ab.length;i++)
    {
        RadioButton radioButtonView = new RadioButton(this);
        radioButtonView.setText(ab[i]);
        radioGroup.addView(radioButtonView, p);
        ((ViewGroup)layout.getParent()).removeView(layout);
    }
String[]ab={“1”,“2”};

对于(int i=0;i如果您有此数组:-

String route[] = {"1", "3", "4"};
可以使用循环创建单选按钮:-

for (int i = 0; i < route.length; i++) 
{
    RadioButton radioButton = new RadioButton(this);
    radioButton.setText("Route " + String.valueOf(route[i]));
    radioButton.setId(i);
    rprms = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
    rgp.addView(radioButton, rprms);
}
for(int i=0;i
您可以使用以下方法获取所选字段:- (我有一个保存按钮,所以这段代码是用onClick listener编写的) 否则,您也可以使用onCheckedChangeListener

int selection = rgp.getCheckedRadioButtonId();
for (int i = 0; i < route.length; i++) 
{
    if (i == selection)
        showToast("" + route[i]);
}
int selection=rgp.getCheckedRadioButtonId();
对于(int i=0;i
这对我来说似乎是开箱即用的:

LinearLayout mLinearLayout=(LinearLayout)findViewById(R.id.linear1);

对于(int k=1;k你做到了,我在过去三天里一直在寻找这个。我的最后一个问题,我需要从顶部给出Marign,我如何给出它?@AshuKumar:你可以通过设置
p.setMargins(左、上、右、下)来设置所有单选按钮的边距
还有一个问题,您能告诉我如何获取创建的值表单RadioButtons,但“radioButtonView”无法识别,您能告诉我我在做什么吗wrong@AshuKumar:请针对这个问题提出其他问题,我的建议是您可以使用
radioGroup.getChildAt(索引)从RadioButton获取文本
您的XML看起来如何?
int selection = rgp.getCheckedRadioButtonId();
for (int i = 0; i < route.length; i++) 
{
    if (i == selection)
        showToast("" + route[i]);
}