【问题标题】:RadioGroup on click fails单击时 RadioGroup 失败
【发布时间】: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,但我有以下问题:

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

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

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

【问题讨论】:

  • 为什么要创建另一个RadioGroup,为什么不直接在主radioGroup 中添加所有RadioButton
  • 为什么要在另一个RadioGroup 中添加一个?删除此RadioGroup account_radios = new RadioGroup(this); 全部添加到radioGroup 并重试。
  • @YaroslavOvdiienko 谢谢这现在可行我看到我的愚蠢错误
  • 没关系,下次只要保持一致并保持代码干净)

标签: java android android-radiobutton android-radiogroup


【解决方案1】:

您正在创建一个新的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);
        }
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多