【问题标题】:How to set value of a radio button inside a radio group android如何在单选组android中设置单选按钮的值
【发布时间】:2015-10-11 00:49:24
【问题描述】:

我正在尝试使用其中有 2 个单选按钮的单选组。我想为每个单选按钮设置一个值,例如该值是男性和女性。之后,当我已经点击了我的单选按钮时,它将显示选择的值。你怎么能做到这一点?感谢您的帮助。

我尝试过这样的事情只是为了测试我将如何设置一个值

public void onRadioButtonClicked(View radioButton) {
    int count = radioGroup.getChildCount();
    for (int i = 0; i < count; i++) {
        View o = radioGroup.getChildAt(i);
        if (o instanceof RadioButton) {

            RadioButton radioBtn =  (RadioButton)o;
            // get the state
            boolean isChecked = radioBtn.isChecked();
            // to set the check
            radioBtn.setChecked(true);

        }
    }
}

但我认为这不是我想要的。

我已经尝试过radioGroup.setId(),假设单选按钮已经有值,但它只是显示无意义的数字。

【问题讨论】:

  • 如果您还没有尝试过任何事情,建议您这样做。可能有帮助的东西:OnCheckedChangeListener
  • 我已经更新了我的问题@Karakuri

标签: android radio-button radio-group


【解决方案1】:

您应该在代码中使用RadioGroupOnCheckedChangeListener 来检测何时选择了RadioButton。在您的布局 XML 中:

<RadioGroup
    android:id="@+id/radioGroup"
    ... >

    <RadioButton
        android:id="@+id/radioButton1"
        ... />

    <RadioButton
        android:id="@+id/radioButton2"
        ... />
</RadioGroup>

在您的 Activity/Fragment 中,设置如下:

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangedListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        <type> value = <default value>;
        switch (checkedId) {
        case R.id.radioButton1:
            value = ...;
            break;
        case R.id.radioButton2:
            value = ...;
            break;
        }
        // do something with value
    }
});

【讨论】:

  • 我有一个错误提示 Cannot resolve method 'setOnCheckedChangedListener()" 我是否需要输入任何导入 @Karakuri
  • 有一个错字,我改正了
【解决方案2】:

确保您的单选按钮包含在如下单选组中:

        <RadioGroup
            android:id="@+id/myRadioGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/radioGroupButtonMale"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content" />

            <RadioButton
                android:id="@+id/radioGroupButtonFemale"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"/>
        </RadioGroup>

在代码中,设置值,

RadioGroup mySelection = (RadioGroup)findViewById(R.id.myRadioGroup);
int radioButtonId = mySelection.getCheckedRadioButtonId();
String selectedValue;
    switch (radioButtonId)
    {
        case R.id.radioGroupButtonMale:
            selectedValue = "Value for Male";
            break;
        case R.id.radioGroupButtonFemale:
            selectedValue = "Value for Female";
            break;
    }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-07
  • 1970-01-01
  • 2017-07-25
  • 2015-06-19
  • 2018-03-08
  • 1970-01-01
  • 2019-01-12
相关资源
最近更新 更多