【发布时间】:2017-11-24 11:04:01
【问题描述】:
我没有看到任何与我的问题类似的帖子。我有一个radiogroup,它有两个按钮,如下所示,
<RadioGroup
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/basic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="@+id/advanced"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
在我的应用程序中,有时我需要一个动态添加的按钮,例如,
button=new AppCompatRadioButton(Activity.this);
button.setId(R.id.my_custom_id);
radiogroup.addView(button);
该无线电组的 Oncheckedchangelistener 代码,
(radioGroup).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId== R.id.basic) {
button.setChecked(false);
//my code
}
else if(checkedId== R.id.advanced) {
button.setChecked(false);
//my code
}
else {
//it is newly added button's part
}
}
});
问题是,当新添加的按钮被勾选时,并没有 单击其他按钮时未选中。我试图解决这个问题
OnCheckedChangeListener通过设置button.setChecked(false);时 检查了其他按钮。但它不起作用。我不知道哪个使 问题。
谁能帮帮我。谢谢!
【问题讨论】:
-
你为所有视图设置了id吗(RadioGroup&RadioButton)?
-
是的,我做到了。对于那个新按钮,我使用了
<resources> <item name="my_custom_id" type="id" /> </resources>i -
分享你的
OnCheckedChangeListener..code -
我已经编辑了我的问题。请看一下
标签: android android-radiogroup