gpt4 book ai didi

java - 单击时 RadioGroup 失败

转载 作者:行者123 更新时间:2023-12-02 01:40:17 25 4
gpt4 key购买 nike

我已经动态地设置了一个广播组,如下所示:

在我的 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,但我有以下问题:

  1. Whenever I click on the second RadioButton the first one cannot be clicked again

  2. the log on the selected listener doesn't log

我不知道出了什么问题,因为 RadioButtons 显示正确。

最佳答案

您正在创建一个新的RadioGroup,并将其放入现有的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);
}
});
}

关于java - 单击时 RadioGroup 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54550080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com