【问题标题】:RadioGroup: How to check programmaticallyRadioGroup:如何以编程方式检查
【发布时间】:2011-12-06 06:59:49
【问题描述】:

我从 XML 创建一个 RadioGroup

    <RadioGroup android:id="@+id/option" 
        android:layout_width="match_parent"
        android:orientation="horizontal" 
        android:checkedButton="@+id/block_scenario_off"
        android:layout_height="wrap_content">
        <RadioButton 
            android:layout_width="0dip"
            android:layout_weight="1" 
            android:text="@string/option1" 
            android:layout_height="wrap_content" 
            android:id="@+id/option1"
            android:layout_gravity="center|left" 
            android:onClick="@string/on_click"/>
        <RadioButton 
            android:layout_width="0dip"
            android:layout_weight="1" 
            android:text="@string/option2" 
            android:onClick="@string/on_click"
            android:layout_height="wrap_content"
            android:layout_gravity="center" 
            android:id="@+id/option2"/>
        <RadioButton 
            android:layout_width="0dip"
            android:layout_weight="1" 
            android:text="@string/option3"
            android:onClick="@string/on_click" 
            android:layout_height="wrap_content"
            android:layout_gravity="center|right" 
            android:id="@+id/option3" />
    </RadioGroup>

在 Java 代码中,我以编程方式检查第一个活动创建 (onCreate()),如下所示:

    mOption = (RadioGroup) findViewById(R.id.option);
    mOption.check(R.id.option1);

但是当显示活动时,没有选中单选按钮。有什么帮助吗?

【问题讨论】:

  • 如果您想以编程方式检查单选按钮,那么您不应该接受这个答案,因为这是我们在 xml 文件中设置它们的检查方式!

标签: android radio-group


【解决方案1】:

在您的布局中,您可以将android:checked="true" 添加到您想要被选中的CheckBox

或者以编程方式,可以使用checkable接口中定义的setChecked方法:

RadioButton b = (RadioButton) findViewById(R.id.option1); b.setChecked(true);

【讨论】:

  • 尝试将该选项添加到 RadioGroup 和/或 RadioButton 级别,不起作用
  • 是的,它适用于在 XML 中分配了选中属性的 RadioButton,但它不适用于 Java 代码(即 mOption.check(R.id.option1));
  • 因为我在同一个布局中有两个 RadioGroup,所以代码不起作用。如果我删除一个,另一个效果很好。我仍然有两个或多个 RadioGroup 在同一布局中的问题。谢谢
  • 这个答案是错误且危险的。设置选中的单选按钮不会修改 RadioGroup 的状态。宝乐在说自己为什么不能让它工作:他在 xml 中重复了两次相同的 id。
  • @AndrewBloom,这不是一个微妙的错误。这是 RadioGroup 文档中的预期行为。
【解决方案2】:

试试这个 如果您希望根据某些变量的值检查您的单选按钮,例如"genderStr" 然后你可以使用下面的代码 sn-p

    if(genderStr.equals("Male"))
       genderRG.check(R.id.maleRB);
    else 
       genderRG.check(R.id.femaleRB);

【讨论】:

    【解决方案3】:

    您可能需要在代码的 onCreate 方法中声明单选按钮并使用它们。

    RadioButton rb1 = (RadioButton) findViewById(R.id.option1);
    rb1.setChecked(true);
    

    【讨论】:

    • 这对我不起作用,mOption.check(R.id.option1);我已经尝试过 onCreate() 和 onResume()。怎么了?
    【解决方案4】:

    小心!使用setChecked() 检查单选按钮不会更改 RadioGroup 内的状态。例如,radioGroup 中的这个方法将返回错误的结果:getCheckedRadioButtonId()

    始终检查 radioGroup

    mOption.check(R.id.option1);
    

    你已经被警告了;)

    【讨论】:

      【解决方案5】:

      我在处理无线电组索引时使用此代码段:

      radioGroup.check(radioGroup.getChildAt(index).getId());
      

      【讨论】:

      • 最佳答案。一条线。容易明白。需要单个对象。可以很容易地添加到方法中。
      • 这应该在顶部。其他的则不必要地复杂。
      【解决方案6】:

      您也可以使用 getChildAt() 方法。像这样:

      mOption = (RadioGroup) findViewById(R.id.option);
      ((RadioButton)mOption.getChildAt(0)).setChecked(true);
      

      【讨论】:

        【解决方案7】:

        抓住广播组,看看孩子们是否有未选中的。

        RadioGroup rg = (RadioGroup) view;
        int checked = savedInstanceState.getInt(wrap.getAttributeName());
        
        if(checked != -1) {
            RadioButton btn = (RadioButton) rg.getChildAt(checked);
            btn.toggle();
        }
        

        【讨论】:

        • 我认为检查必须是 >-1 而不是 !=-1
        【解决方案8】:

        我在为无线电组使用 getId() 时使用此代码:

        radiogroup.check(radiogroup.getChildAt(0).getId());
        

        根据您对 RadioGroup 的设计设置位置

        希望对您有所帮助!

        【讨论】:

          【解决方案9】:

          如果你把你的mOption.check(R.id.option1); 放到onAttachedToWindow 方法中或者像这样:

          view.post(new Runnable()
          {
              public void run() 
              {
                  // TODO Auto-generated method stub
                  mOption.check(R.id.option1); 
              }
          });
          

          原因可能是 check 方法仅在实际呈现 radiogroup 时才有效。

          【讨论】:

          • 大卫在他的回答中评论说,在实际渲染无线电组之前,检查方法将不起作用,结果证明这为我解决了一个谜。
          【解决方案10】:

          我更喜欢使用

          RadioButton b = (RadioButton) findViewById(R.id.option1);
          b.performClick();
          

          而不是使用接受的答案。

          RadioButton b = (RadioButton) findViewById(R.id.option1);
          b.setChecked(true);
          

          原因是setChecked(true) 只改变了单选按钮的选中状态。它不会触发添加到单选按钮的onClick() 方法。有时这可能会浪费您的时间来调试与onClick() 相关的操作为何不起作用。

          【讨论】:

            【解决方案11】:

            在 Kotlin 中。选择第一个元素。您需要在 onViewCreated 或更高版本中执行此操作。

            radioGroup.apply { check(getChildAt(0).id) }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-11-24
              • 1970-01-01
              • 2012-06-14
              • 2012-03-10
              • 2012-08-28
              • 2010-09-18
              • 1970-01-01
              • 2021-09-22
              相关资源
              最近更新 更多