【发布时间】:2019-02-06 18:03:33
【问题描述】:
我已经像这样动态地设置了一个广播组:
在我的 XML 中有:
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/user_accounts_radios"
android:layout_gravity="left"
android:layout_marginLeft="20dp"
android:layout_height="wrap_content"
android:orientation="vertical">
在我的 Java 代码中,我有
RadioGroup radioGroup;
protected void onCreate(){
radioGroup = (RadioGroup) findViewById(R.id.user_accounts_radios);
setupUsers();
}
关于 setupUsers
void displayOpts(){
List<UserAccountDetailsModel> accounts = userAccountDetailsSqliteModel.getAccounts();
RadioGroup account_radios = new RadioGroup(this);
account_radios.setOrientation(LinearLayout.VERTICAL);
for (UserAccountDetailsModel user : accounts){
CompanyLocationsModel company = companyLocationSqliteModel.getCompany(user.getCompany_id());
RadioButton rdbtn = new RadioButton(this);
rdbtn.setTextSize(17);
rdbtn.setId(company.getId());
rdbtn.setText(user.getFirst_name() + " "+user.getLast_name() + " ---- " + company.getName());
account_radios.addView(rdbtn);
}
radioGroup.addView(account_radios);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
Log.i("test", "Checked is "+checkedId);
}
});
}
上面显示RadioGroup,但我有以下问题:
当我点击第二个
RadioButton时,第一个无法点击 再次所选侦听器上的日志未记录
我不知道怎么了,因为RadioButtons显示正确。
【问题讨论】:
-
为什么要创建另一个
RadioGroup,为什么不直接在主radioGroup中添加所有RadioButton -
为什么要在另一个
RadioGroup中添加一个?删除此RadioGroup account_radios = new RadioGroup(this);全部添加到radioGroup并重试。 -
@YaroslavOvdiienko 谢谢这现在可行我看到我的愚蠢错误
-
没关系,下次只要保持一致并保持代码干净)
标签: java android android-radiobutton android-radiogroup