【问题标题】:How to get default selected radio button from radio group?如何从单选组中获取默认选择的单选按钮?
【发布时间】:2019-10-01 01:21:51
【问题描述】:

我有一个由三个单选按钮 'yes','no','do not know' 组成的单选组。我将android:checkedButton="@id/r3" 设置为默认选择第三个单选按钮。我已经为单选组设置了setOnCheckedChangeListener,它会在从 UI 中选择任何单选按钮时被调用。

我想为默认选中的单选按钮调用onCreate 中的侦听器,因为我正在在默认选中的代码中设置一些值。

是否有可能在onCreate 的单选组中获取默认选中的单选按钮并执行一些操作,稍后setOnCheckedChangeListener 将处理 UI 中选中的单选按钮

<RadioGroup
                    android:id="@+id/rg1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:checkedButton="@id/r3"
                    android:orientation="horizontal"
                    android:weightSum="3">

                    <RadioButton
                        android:id="@+id/r1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Yes"
                        android:textColor="@color/fontBlackEnable" />

                    <RadioButton
                        android:id="@+id/r2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="No"
                        android:textColor="@color/fontBlackEnable" />

                    <RadioButton
                        android:id="@+id/r3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:checked="true"
                        android:text="@string/do_not_know" />

                </RadioGroup>
public class MyFragment extends Fragment {

    View rootView;
    Context context;
    RadioGroup mRadioAns3;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rootView = inflater.inflate(R.layout.my_fragment, container, false);
        context = container.getContext();
        mRadioAns3 = rootView.findViewById(R.id.rg1);

       //Todo: get default selected radiobutton and show toast on load of fragment


        mRadioAns3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Toast.makeText(context,((RadioButton) rootView.findViewById(checkedId)).getText() ,Toast.LENGTH_LONG ).show();
            }
        });

        return rootView;
    }


}

【问题讨论】:

  • 只需使用 android:checked="true"

标签: android radio-button radio-group


【解决方案1】:
if(gender.getCheckedRadioButtonId()==-1)
{
    Toast.makeText(mContext, "Please select Gender", Toast.LENGTH_SHORT).show();
}
else
{
    // get selected radio button from radioGroup
    int selectedId = gender.getCheckedRadioButtonId();
    // find the radiobutton by returned id
    selectedRadioButton = (RadioButton)findViewById(selectedId);
    Toast.makeText(mContext, selectedRadioButton.getText().toString()+" is selected", Toast.LENGTH_SHORT).show();
}

【讨论】:

  • gender 是你的RadioGroup
【解决方案2】:

替换这个:

android:checkedButton="@+id/r3"

代替:

android:checkedButton="@id/r3"

你也可以使用这个:

radiogroup.check(radioButtonId)

【讨论】:

    【解决方案3】:

    布尔属性android:checked是布局文件中可用的帮助属性,

    类似的,

    <RadioGroup
        android:id="@+id/rgGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
        <RadioButton
            android:id="@+id/rbMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" 
            android:checked="true" />
    
        <RadioButton
            android:id="@+id/rbFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />
    </RadioGroup>
    

    注意:将此属性分配给您希望默认显示为选中的RadioButton

    【讨论】:

      【解决方案4】:

      您可以通过其ID 获取默认选定按钮。 RadioGroup 支持返回默认选中单选按钮的按钮id的方法。

      在你的情况下,代码就像

      rg1.getCheckedRadioButtonId()
      

      方法请查看android文档。

      getCheckedRadioButtonId

      【讨论】:

        【解决方案5】:

        单选组支持查找选中的单选按钮id,这里你的单选按钮是mRadioAns3,你可以通过获取选中单选按钮的id,

        int selectedRadioBtnID = mRadioAns3.getCheckedRadioButtonId ();
        

        通过使用这个id,你可以获得选中的单选按钮,

        RadioButton radioBtn = mRadioAns3.findViewByID(selectedRadioBtnID);
        

        通过这个单选按钮,您可以在加载时显示 toast 消息,

        Toast.makeText(context, radioBtn.getText(),Toast.LENGTH_LONG ).show();
        

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 2018-09-04
          • 2013-03-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-16
          • 1970-01-01
          • 2018-12-26
          • 1970-01-01
          • 2021-05-13
          相关资源
          最近更新 更多