android - radioButton返回id

时间:2018-06-08 10:48:26

标签: java android

在我的应用程序中,我有一个包含三个radioButtons的radioGroup的片段。我为每个radioButton设置了一个ID,点击一个按钮后,我想将所选的id传递给textView。现在它每次都返回值-1,因此没有选择任何按钮。为什么不返回正确的值?

我的代码:

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代码:

<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>

3 个答案:

答案 0 :(得分:0)

  

您需要在RadioGroup上使用setOnCheckedChangeListener作为Follow而不是Button click事件。

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);
    testText = (TextView)findViewById(R.id.textView5);
}

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

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


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

    rg .setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
       x = checkedId;
       testText.setText(x.toString());
    }
});

    return view;
}

}

答案 1 :(得分:0)

尝试这个...

...

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);

...

答案 2 :(得分:-1)

从radioGroup获取选定的单选按钮,请尝试使用它,这对我有用。

        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();