gpt4 book ai didi

Android - 将 RadioButton 动态添加到 RadioGroup

转载 作者:行者123 更新时间:2023-11-29 15:47:24 30 4
gpt4 key购买 nike

我正在尝试将单选按钮动态添加到单选组中,但我无法将单选按钮添加到其中。 (它在 radio 组内没有显示 radio 按钮)代码没有显示错误。

我从今天早上开始就一直在寻找解决方案。非常感谢您的帮助!!

private void createCustomDialog(){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

final Dialog dialog = new Dialog(this);
dialog.setTitle("Choose Device");
dialog.setContentView(R.layout.dialog_choose_device);

LinearLayout parent = (LinearLayout)dialog.findViewById(R.id.linearLayoutCD);

LinearLayout li = new LinearLayout(this);
li.setLayoutParams(params);
li.setOrientation(LinearLayout.VERTICAL);



RadioGroup rg = (RadioGroup)dialog.findViewById(R.id.rgCD);
RadioButton[] rbArray = new RadioButton[bdList.size()];

for(int i = 0; i < bdList.size(); i++){
rbArray[i] = new RadioButton(this);
rbArray[i].setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
rbArray[i].setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
rbArray[i].setText(bdList.get(i).getName());
rbArray[i].setId(i);
Log.d(LOG, "" + bdList.get(i).getName());
li.addView(rbArray[i]);

}

Button btnOK = new Button(this);


btnOK.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){

dialog.dismiss();
}
});

((ViewGroup) dialog.findViewById(R.id.rgCD)).addView(li);

dialog.show();
}

我的对话框 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/linearLayoutCD"
android:orientation="vertical">
<RadioGroup
android:id="@+id/rgCD"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">


</RadioGroup>

<Button
android:id="@+id/btnOK"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK"
/>


</LinearLayout>

最佳答案

试试这个:

RadioGroup rg = (RadioGroup) findViewById(R.id.RadioGroup);

RadioButton radioButton = new RadioButton(this);
radioButton.setText("radio text");
radioButton.setId(1234);//set radiobutton id and store it somewhere
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
rg.addView(radioButton, params);

关于Android - 将 RadioButton 动态添加到 RadioGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32358657/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com