java - 单击时 RadioGroup 失败
问题描述
我已经像这样动态地设置了一个广播组:
在我的 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
的并将其放入现有的中。这是错误的。只需像这样使用现有的:
void displayOpts(){
List<UserAccountDetailsModel> accounts = userAccountDetailsSqliteModel.getAccounts();
//deleted line
//next line changed
radioGroup.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());
//next line changed
radioGroup.addView(rdbtn);
}
//deleted line
//next line changed
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
Log.i("test", "Checked is "+checkedId);
}
});
}
推荐阅读
- python - 如何在张量流中重用占位符?
- postgresql - 将行从主表移动到具有外键约束的子表
- vba - 从 PST 导入日历/联系人 - Outlook VBA
- python - 检查目标时出错:预期的 dense_49 有 4 个维度,但得到了形状为 (2250, 3) 的数组
- jquery-file-upload - jQuery文件上传多个文件名
- javascript - 为什么获取请求在数组映射中不起作用?
- sql-server - SQL Server 2012 - 在插入之前进行结构验证
- caching - 当指令不在 L1I 中时,IFU 和前端会发生什么?
- python - Django 应用程序在迁移到 heroku MySQL ClearDB 时抛出 MigrationSchemaMissing
- python - 杀死一个进程,等待一段随机时间然后再次启动该进程?