【问题标题】:How can get selected Radio Button from Radio Group from a fragment inside activity?如何从活动中的片段中从 Radio Group 中获取选定的 Radio Button?
【发布时间】:2018-05-18 04:27:33
【问题描述】:

我是 android 中的新手,我无法获取从活动内片段上的 radiogroup 获取选定值的逻辑。 我想为我的班级做一个简单的提问者。

这是我的java

public class Frag3_Kepentingan extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view_frag3 = inflater.inflate(R.layout.activity_frag3_kepentingan, container, false);
    return view_frag3;

我已经为每个单选按钮和单选组提供了 ID。 这是我的 xml_Layout

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="290dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="58dp">

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

                <RadioButton
                    android:id="@+id/grup31A1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1"
                    android:layout_weight="1" />

                <RadioButton
                    android:id="@+id/grup31A2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2"
                    android:layout_weight="1" />

                <RadioButton
                    android:id="@+id/grup31A3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3"
                    android:layout_weight="1" />

       </RadioGroup>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true">

        <Button
            android:id="@+id/btnSimpanGrup3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_weight="1"
            android:text="Simpan" />

    </RelativeLayout>
</LinearLayout>

感谢您的分享。

【问题讨论】:

  • 为片段中的无线电检查/取消检查创建获取和设置方法,现在在活动中访问这些方法!

标签: android android-fragments android-radiogroup


【解决方案1】:
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {


            switch (checkedId)
            {

                //your desire code
            }
        }
    });

【讨论】:

    【解决方案2】:

    RadioGroup 布局应如下所示

    <RadioGroup
        android:id="@+id/myRadioGroup"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
    
        <RadioButton
            android:id="@+id/rbMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male"
            android:textColor="@color/colorPrimaryDark"
            android:buttonTint="@color/colorPrimaryDark" />
    
        <RadioButton
            android:id="@+id/rbFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:layout_marginRight="20sp"
            android:layout_marginLeft="20sp"
            android:textColor="@color/colorPrimaryDark"
            android:buttonTint="@color/colorPrimaryDark" />
    
      </RadioGroup>
    

    在你的 Fragment 中初始化 RadioButtons

    RadioButton rMale = getActivity().findViewById(R.id.rbMale);
    RadioButton rFemale = getActivity().findViewById(R.id.rbFemale);
    

    如果选中或未选中,则从 RadioButton 获取值

     String userGender= "";
                    if (rMale.isChecked()) {
                        userGender = "male";
                    } else if (rFemale.isChecked()) {
                        userGender = "female";
                    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2018-08-07
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 2017-11-23
      相关资源
      最近更新 更多