首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >android - radioButton返回id

android - radioButton返回id
EN

Stack Overflow用户
提问于 2018-06-08 18:48:26
回答 2查看 85关注 0票数 1

在我的应用程序中,我有一个包含三个radioButtons的radioGroup片段。我为每个radioButton设置了一个ID,在单击一个按钮之后,我希望将选择的id传递给一个textView。现在,它每次都返回值-1,所以就像没有选择按钮一样。为什么它不返回正确的值呢?

我的代码:

代码语言:javascript
复制
public class ItemThreeFragment extends Fragment {

    TextView testText;
    int x;

    public static ItemThreeFragment newInstance() {
        ItemThreeFragment fragment = new ItemThreeFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_item_three, container, false);

        final int rb1id = 0;
        final int rb2id = 1;
        final int rb3id = 2;

        final RadioGroup rg = (RadioGroup) getActivity().findViewById(R.id.RGroup);
        RadioButton rb1 = (RadioButton) getActivity().findViewById(R.id.radioButton);
        RadioButton rb2 = (RadioButton) getActivity().findViewById(R.id.radioButton2);
        RadioButton rb3 = (RadioButton) getActivity().findViewById(R.id.radioButton3);

        rb1.setId(rb1id);
        rb2.setId(rb2id);
        rb3.setId(rb3id);

        Button button = (Button) view.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                x = rg.getCheckedRadioButtonId();
                testText = getActivity().findViewById(R.id.textView5);
                testText.setText(String.valueOf(x));

            }
        });

        return view;
    }

}

xml代码:

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:id="@+id/RGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:gravity="center"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:layout_marginRight="30dp"
            android:layout_toLeftOf="@id/radioButton"
            android:text="E5" />

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            android:layout_centerHorizontal="true"
            android:text="E10" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/radioButton"
            android:layout_marginLeft="30dp"
            android:layout_toEndOf="@+id/radioButton"
            android:layout_toRightOf="@id/radioButton"
            android:text="Diesel" />
    </RadioGroup>


    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView11"
        android:layout_centerHorizontal="true"
        android:onClick="onClick"
        android:text="Button" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:text="TextView" />

</RelativeLayout>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-09 15:06:48

试试这个..。

代码语言:javascript
复制
...

final RadioGroup rg = (RadioGroup) view.findViewById(R.id.RGroup);

RadioButton rb1 = (RadioButton) view.findViewById(R.id.radioButton);
RadioButton rb2 = (RadioButton) view.findViewById(R.id.radioButton2);
RadioButton rb3 = (RadioButton) view.findViewById(R.id.radioButton3);

...
票数 0
EN

Stack Overflow用户

发布于 2018-06-09 14:59:04

从radioGroup获取选中的单选按钮,请尝试这个,它对我很有效。

代码语言:javascript
复制
        int selectedId = radioGroup.getCheckedRadioButtonId();

        // find the radiobutton by returned id
   RadioButton radioButton = (RadioButton) findViewById(selectedId);

        Toast.makeText(MainActivity.this,
            radioButton.getText(), Toast.LENGTH_SHORT).show();
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50758975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档