0

在我的 android 应用程序中,我尝试使用以下代码在单选组内根据 arraylist 大小创建一个动态单选按钮

for (int aa = 0; aa < itemname.length(); aa++)
{
    String iname = itemname.getJSONObject(aa).getString("item");

    final RadioButton[] radioButton = new RadioButton[itemname.length()];
    radioButton[aa] = new RadioButton(this);
    radioGroup.addView(radioButton[aa]);
    radioButton[aa].setTextColor(R.color.black);
    radioButton[aa].setText(iname);
    radioButton[aa].setButtonDrawable(R.drawable.about_us_tab);                       

    radioButton[aa].setId(h_count);
    radioGroup.setOnCheckedChangeListener(new     android.widget.RadioGroup.OnCheckedChangeListener()
     {   
         public void onCheckedChanged(RadioGroup group, int checkedId)
         {   
             int selectedBtn = radioGroup.getCheckedRadioButtonId();
          }
       });
       h_count++;
      }
  }

我以正确的顺序获取所有数据,并且无线电组的 onclick 操作也正常工作。但我希望第一个单选按钮处于选定模式。为此,我尝试使用如下 if 条件

if(aa == 0)
    {
        radioButton[aa].setChecked(true);
        radioButton[aa].setId(h_count);
    }

但这里的问题是,第一个被选中,并且在无线电组的 onclick 操作中,第一个没有被取消选中,所有其他的都工作正常。

如何在单选组的 onclick 动作中取消选择第一个

4

1 回答 1

0

以这种方式尝试及其工作

for (int aa = 0; aa < itemname.length(); aa++)
{
    String iname = itemname.getJSONObject(aa).getString("item");

    final RadioButton radioButton = new RadioButton(this);
    radioGroup.addView(radioButton;
    radioButton.setTextColor(R.color.black);
    radioButton.setText(iname);
    radioButton.setButtonDrawable(R.drawable.about_us_tab);                       

    if(aa == 0)
    {
        radioButton.setChecked(true);
    }

    radioButton.setId(h_count);
    radioGroup.setOnCheckedChangeListener(new     android.widget.RadioGroup.OnCheckedChangeListener()
    {   
         public void onCheckedChanged(RadioGroup group, int checkedId)
         {   
             int selectedBtn = radioGroup.getCheckedRadioButtonId();
         }
    });
    h_count++;
}
于 2012-06-28T05:42:19.283 回答