Android-将RadioButton动态添加到RadioGroup中
我试图将radiobutton动态添加到radiogroup中,但无法将radiobutton添加到其中。(它不显示radiogroup内的radiobutton) 代码没有显示错误 从今天早上开始,我一直在寻找解决方案。 非常感谢你的帮助Android-将RadioButton动态添加到RadioGroup中,android,Android,我试图将radiobutton动态添加到radiogroup中,但无法将radiobutton添加到其中。(它不显示radiogroup内的radiobutton) 代码没有显示错误 从今天早上开始,我一直在寻找解决方案。 非常感谢你的帮助 private void createCustomDialog(){ LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParam
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();
}
private void createCustomDialog(){
LinearLayout.LayoutParams params=新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容);
最终对话框=新对话框(本);
对话框.setTitle(“选择设备”);
setContentView(R.layout.dialog\u选择设备);
LinearLayout父项=(LinearLayout)dialog.findViewById(R.id.linearLayoutCD);
LinearLayout li=新的LinearLayout(本);
li.setLayoutParams(params);
li.设置方向(线性布局。垂直);
RadioGroup rg=(RadioGroup)dialog.findViewById(R.id.rgCD);
RadioButton[]rbArray=新的RadioButton[bdList.size()];
对于(int i=0;i
对话框的我的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);
试试这个:
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);
这个答案解决的问题是,您需要将单选按钮添加到RadioGroup,而不是LinearLayout。顺便说一句,当我尝试此技术时,RadioGroup的“多重排除范围”不起作用。换句话说,当我选中第二个单选按钮时,第一个单选按钮本应被清除,但它保持选中状态。此答案解决的问题是,您需要将单选按钮添加到RadioGroup,而不是LinearLayout。顺便说一句,当我尝试此技术时,RadioGroup的“多重排除范围”不起作用。换句话说,当我选中第二个单选按钮时,第一个按钮本应被清除,但它保持选中状态。