单击的RadioGroup失败

时间:2019-02-06 09:16:16

标签: java android android-radiobutton android-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,但是我遇到以下问题:

  
      
  1. 每当我单击第二个RadioButton时,都无法单击第一个   再次

  2.   
  3. 所选侦听器上的日志未记录

  4.   

我不知道怎么了,因为RadioButtons显示正确。

1 个答案:

答案 0 :(得分:2)

您正在创建一个新的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); } }); } 中。错了只需使用现有的这样的话:

localhost:8000/foo?param1=yeaa&param2=yeaa2