【问题标题】:How to set a radio button in Android如何在 Android 中设置单选按钮
【发布时间】:2011-05-07 06:46:16
【问题描述】:

我有一个使用单选按钮的应用。此按钮的默认设置在 main.xml 文件中,即:

android:id="@+id/rb_sat1E"
android:checked="true"

在我拥有的 Java 文件中:

final RadioButton radio1 = (RadioButton)findViewById(R.id.rb_sat1E);

我还在 Java 主文件中创建了一个“重置”按钮,可以使用以下代码重置 TextView 信息,即。

pos1_deg.setText("0.0");

但是如何重置单选按钮?我本来以为是这样的

radio1.setBoolean("TRUE");

但这根本不起作用。

非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: android radio-button


    【解决方案1】:

    单选按钮使用

    radio1.setChecked(true);
    

    只有一个 RadioButton 是没有意义的。如果您有更多,则需要通过

    取消选中其他人
    radio2.setChecked(false); ...
    

    如果您的设置只是开/关,请使用 CheckBox。

    【讨论】:

    • 对于可能发现自己处于我的位置的其他读者:setChecked (true) 与 setSelected (true) 完全不同。确认。
    【解决方案2】:

    如果你想在代码中做,可以调用 RadioGroup 的 check 成员:

    radioGroup.check(R.id.radioButtonId);
    

    这将检查您指定的按钮并取消选中其他按钮。

    【讨论】:

    • 谢谢 :) 这对我来说是更好的答案;正是我想要的。
    • radioGroup.check(radioGroup.getChildAt(0).getId()); - 检查列表中的第一项
    • 我同意。请这样做。
    【解决方案3】:

    或者您可以在 XML 文件中执行此操作:

    RadioGroup 中使用:android:checkedButton="id_button_to_check"

    或在 RadioButton 中:android:checked="true"

    【讨论】:

    • 当我在 xml 中选中单选按钮时,我以后无法取消选中它。就像从组中删除了单选按钮
    • 对不起,我迟到了,但这是单选按钮的预期行为。当您选择它时,您无法取消选择它,除非选择无线电组中的另一个单选按钮
    • 我们可以通过调用 check(-1) 或 clearCheck() 来清除选择。
    【解决方案4】:

    澄清一下:如果我们有一个RadioGroup 和几个RadioButtons 并且需要通过索引激活一个,则意味着:

    radioGroup.check(R.id.radioButtonId)
    

    radioGroup.getChildAt(index)`
    

    我们可以这样做:

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

    【讨论】:

      【解决方案5】:

      如果您已经在 XML 中完成了设计,并且希望在加载页面时将组中的复选框之一显示为选中状态,则以下解决方案可以帮助您

          <RadioGroup
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@+id/txtLastNameSignUp"
              android:layout_margin="20dp"
              android:orientation="horizontal"
              android:id="@+id/radioGroup">
      <RadioButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:checked="true"
          android:id="@+id/Male"
          android:text="Male"/>
              <RadioButton
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/Female"
                  android:text="Female"/>
              </RadioGroup>
      

      【讨论】:

        【解决方案6】:

        很多时候,如果您的单选按钮属于同一个 radioGroup,那么

        radioButton.setChecked(true)

        不会正确选择单选按钮。因此,要解决此问题,请尝试使用您的 radioGroup。

        radioGroup.check(R.id.radioButtonId)

        【讨论】:

          【解决方案7】:

          使用此代码

              ((RadioButton)findViewById(R.id.radio3)).setChecked(true);
          

          错误 -> 不要忘记在 setChecked() 之前给整个 () -> 如果你忘记了 setChecked() 对这个单选按钮不可用

          【讨论】:

            【解决方案8】:

            我有多个没有组的 RadioButtons,setChecked(true) 有效,但 setChecked(false) 无效。但是这段代码有效:

            RadioButton switcher = (RadioButton) view.findViewById(R.id.active);
                                    switcher.setOnClickListener(new RadioButton.OnClickListener(){
                                        @Override
                                        public void onClick(View v) {
                                            if(((RadioButton)v).isSelected()){
                                                ((RadioButton)v).setChecked(false);
                                                ((RadioButton)v).setSelected(false);
                                            } else {
                                                ((RadioButton)v).setChecked(true);
                                                ((RadioButton)v).setSelected(true);
                                            }
                                        }
            
                                    });
            

            【讨论】:

              【解决方案9】:
              btnDisplay.setOnClickListener(new OnClickListener() {
              
                  @Override
                  public void onClick(View v) {
              
                          // get selected radio button from radioGroup
                      int selectedId = radioSexGroup.getCheckedRadioButtonId();
              
                      // find the radiobutton by returned id
                          radioSexButton = (RadioButton) findViewById(selectedId);
              
                      Toast.makeText(MyAndroidAppActivity.this,
                          radioSexButton.getText(), Toast.LENGTH_SHORT).show();
              
                  }
              
              });
              

              【讨论】:

              • 你能解释一下为什么 OP 应该使用你的代码吗?
              猜你喜欢
              • 1970-01-01
              • 2015-10-11
              • 2021-11-13
              • 2021-04-08
              • 1970-01-01
              • 2010-09-21
              • 1970-01-01
              • 2017-10-15
              • 2016-11-28
              相关资源
              最近更新 更多