【问题标题】:Set selected index of an Android RadioGroup设置 Android RadioGroup 的选定索引
【发布时间】:2012-04-08 05:00:39
【问题描述】:

除了循环遍历子单选按钮并选择检查所选索引处的单选按钮之外,有没有办法在 android 中设置 RadioGroup 的选定索引?

注意:我在运行时填充单选按钮组。

【问题讨论】:

    标签: android android-radiogroup android-radiobutton


    【解决方案1】:

    使用 Kotlin 你可以通过

      (radio_group_id.getChildAt(index) as RadioButton).isChecked = true
    

    radio_group_id.check(R.id.radio_button_id)

    【讨论】:

      【解决方案2】:

      如果您的单选组是在布局 xml 文件中定义的,则可以为每个按钮分配一个 id。然后你只需检查一个这样的按钮

      radioGroup.check(R.id.myButtonId);
      

      如果您以编程方式创建了无线电组(我什至不确定您是如何执行此操作的...),您可能需要考虑为无线电组创建一个特殊的布局 xml 文件,以便您可以分配 R.id .* 按钮的 ID。

      如果您实际上希望按索引设置单选按钮组,请查看下面的答案,请参见下面的答案。

      ((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
      

      【讨论】:

      • 嗨,我使用以下代码以编程方式创建了它们:for (int i = 0; i
      • 由于您将它们的 id 设置为 lookupTypes 中的索引,因此您可以将该索引用作 check 方法的参数。
      • 这个答案实际上只是再次解释了提问者已经说过的内容 - 另一个答案应该是公认的答案,因为它正确地说明了如何通过索引进行选择。
      • @LassiKinnunen 我觉得这个答案的赞成票数量表明人们正在从中得到他们需要的东西......也就是说,你是对的,对于所提出的问题,另一个答案更正确:-/
      • 老实说,这是我投票率最高的 SO 答案,我感到有点尴尬。
      【解决方案3】:

      在onBindViewHolder里面设置标签为Button Group

      @Override
      public void onBindViewHolder(final CustomViewHolder holder, final int position) {
         ...
         holder.ButtonGroup.setTag(position);
      }
      

      在 ViewHolder 中

      ButtonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
          @Override
          public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
              ...
              int id = radioGroup.getCheckedRadioButtonId();
      
              RadioButton radioButton = (RadioButton) radioGroup.findViewById(id);
      
              int clickedPos = (Integer) radioGroup.getTag();
      
              packageModelList.get(clickedPos).getPoll_quest()
          }
      

      【讨论】:

        【解决方案4】:

        问题说“设置选定的索引”,这里是如何通过索引设置它:

        ((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
        

        ........

        附加信息:Google 似乎希望您使用 id 而不是索引,因为使用 id 更能证明错误。例如,如果您的 RadioGroup 中有另一个 UI 元素,或者如果另一个开发人员重新订购 RadioButtons,则索引可能会发生变化,而不是您所期望的。但如果你是唯一的开发人员,这完全没问题。

        【讨论】:

        • 你能解释一下这段代码,它是正确的答案吗?
        • @rfornal 此代码允许您通过 INDEX 获取单选按钮,而不是通过 View id(例如 R.id.radioButton1),并减少了实现查找表以转换索引的需要查看id。
        【解决方案5】:

        这对我有用,我通过

        动态创建了单选按钮
          private void createRadioButton() {
        
            RadioButton[] rb = new RadioButton[5];
        
            RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    1.0f);
        
            radioGroup.setOrientation(RadioGroup.HORIZONTAL);
        
            for (int ID = 0; ID < 5; ID++) {
                rb[ID] = new RadioButton(this);
                rb[ID].setLayoutParams(layoutParams);
                rb[ID].setText("Button_Text");
                radioGroup.addView(rb[ID]); //the RadioButtons are added to the radioGroup instead of the layout
            }
        }
        

        现在检查一个按钮,

        int radio_button_Id = radioGroup.getChildAt(index).getId();
        
        radioGroup.check( radio_button_Id );
        

        【讨论】:

          【解决方案6】:

          Siavash 的回答是正确的:

          ((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
          

          但请注意,radioGroup 可以包含除 radioButtons 以外的视图 - 就像这个示例,在每个选项下都包含一条微弱的线。

          <RadioGroup
              android:id="@+id/radioKb"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >
          
              <RadioButton
                  android:id="@+id/kb1"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceLarge"
                  android:button="@null"
                  android:drawableRight="@android:drawable/btn_radio"
                  android:text="Onscreen - ABC" />
          
              <View
                  android:layout_width="fill_parent"
                  android:layout_height="1dp"
                  android:background="#33000000" />
          
              <RadioButton
                  android:id="@+id/kb2"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceLarge"
                  android:button="@null"
                  android:drawableRight="@android:drawable/btn_radio"
                  android:text="Onscreen - Qwerty" />
          
              <View
                  android:layout_width="fill_parent"
                  android:layout_height="1dp"
                  android:background="#33000000" />
          
              <RadioButton
                  android:id="@+id/kb3"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceLarge"
                  android:button="@null"
                  android:drawableRight="@android:drawable/btn_radio"
                  android:text="Standard softkey" />
          
              <View
                  android:layout_width="fill_parent"
                  android:layout_height="1dp"
                  android:background="#33000000" />
          
              <RadioButton
                  android:id="@+id/kb4"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:textAppearance="?android:attr/textAppearanceLarge"
                  android:button="@null"
                  android:drawableRight="@android:drawable/btn_radio"
                  android:text="Physical keyboard" />
          
              <View
                  android:layout_width="fill_parent"
                  android:layout_height="1dp"
                  android:background="#33000000" />
          
          </RadioGroup>
          

          在这种情况下,例如,使用索引 1 会产生错误。索引 1 处的项目是第一个分隔线——不是单选按钮。此示例中的单选按钮位于索引 0、2、4、6 处。

          【讨论】:

            【解决方案7】:

            您可以从广播组中查找ViewById。

            ((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);`
            

            【讨论】:

              猜你喜欢
              • 2011-09-20
              • 2018-05-30
              • 2016-08-14
              • 2021-03-02
              • 1970-01-01
              • 1970-01-01
              • 2013-03-10
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多