我是android的新手,我有一个像这样的xml:
<RadioGroup
android:id="@+id/cardRadioGroup"
android:layout_width="wrap_content"
android:layout_height="45dp" android:layout_alignParentLeft="true" android:layout_marginLeft="35dp"
android:layout_alignTop="@+id/cardEditText">
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)
我正在使用我的代码来膨胀这个xml,根据我的数据模型生成我想要的任意数量的单选按钮.
我使用的代码是(在for循环中):
LayoutInflater inflater = this.getLayoutInflater();
View currentView = inflater.inflate(R.layout.card_payment_content_node,null);
RadioGroup rg = (RadioGroup) currentView.findViewById(R.id.cardRadioGroup);
RadioButton rb = new RadioButton(this);
//set radiobutton properties
rb.setText(entry.getKey());
rb.setId(++radioButtonIdCounter);
rg.addView(rb);
//add to view
parent.addView(currentView);
Run Code Online (Sandbox Code Playgroud)
这是按预期工作的.但问题是我可以在设备上一次选择多个单选按钮.
我不确定为什么会这样.我在哪里弄错了?
提前致谢.
所有按钮都具有相同的ID.这就是他们如此统一行事的原因.
您只需将布局膨胀一次.您必须通过识别cardRadioGroup然后循环代码,以编程方式添加按钮,为所有按钮生成唯一ID.
要向组中添加单选按钮,请使用RadioGroup.addview().
要创建单选按钮,请使用RadioButton的一个构造函数,并根据需要设置其他属性.
| 归档时间: |
|
| 查看次数: |
2893 次 |
| 最近记录: |