【问题标题】:android - dynamic first radio button to be in true stateandroid - 动态第一个单选按钮处于真实状态
【发布时间】:2012-06-26 14:23:08
【问题描述】:

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

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 动作中也被取消选择

【问题讨论】:

    标签: android dynamic radio-button radio-group


    【解决方案1】:

    以这种方式尝试和它的工作

    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++;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-24
      • 1970-01-01
      • 2020-05-07
      • 2022-08-15
      • 2019-08-21
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多