处理片段中的 radio 组和按钮

2019年6月24日 13点热度 0条评论

首先看一下我的片段xml文件的一部分

  <LinearLayout
            android:layout_weight="1"
            android:background="#aae4e4e4"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="rbclick"
                    android:text="0" />

                <RadioButton


                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:onClick="rbclick"
                    android:text="1" />

                <RadioButton
                    android:id="@+id/radio2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:onClick="rbclick"
                    android:text="2" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:onClick="rbclick"
                    android:text="3" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:onClick="rbclick"
                    android:text="4" />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:onClick="rbclick"
                    android:text="5" />


                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:onClick="rbclick"
                    android:text="6" />
            </RadioGroup>


        </LinearLayout>

现在来看一下我的片段Java类

 public class MealEntry extends Fragment {

    private MealEntryViewModel mViewModel;

    private DatePickerDialog.OnDateSetListener mDateSetListener;
    private static final String TAG = "MealEntry";
    public RadioGroup rg;
    RadioButton rb;


    public static MealEntry newInstance() {
        return new MealEntry();
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.meal_entry_fragment, container, false);


         rg  = view.findViewById(R.id.group_no_1);
     return view;
    }


        public void rbclick (View v){

        int radiobtnid=rg.getCheckedRadioButtonId();
        rb=(RadioButton)findViewById(radiobtnid);

    }

之前通过此过程,我的意思是(rbclick function)可以处理Activity中的单选组和单选按钮(我重复Activity)。但在Fragment中,onClick方法不起作用。我该如何解决?我已经尝试过这种方式的片段。

  public void rbclick (View v){

        int radiobtnid=rg.getCheckedRadioButtonId();
        rb =(RadioButton)getActivity().findViewById(radiobtnid);

    }

我认为此处理程序不起作用。当我单击单选按钮时,它会崩溃。

这是原木猫。

    12-18 22:29:07.715 3720-3720/com.example.closeup W/PathParser: Points are too far apart 4.000000596046461
12-18 22:29:11.963 3720-3720/com.example.closeup W/ResourceType: No package identifier when getting name for resource number 0x00000016
12-18 22:29:11.964 3720-3720/com.example.closeup D/AndroidRuntime: Shutting down VM
12-18 22:29:11.972 3720-3720/com.example.closeup E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.closeup, PID: 3720
    android.content.res.Resources$NotFoundException: Unable to find resource ID #0x16
        at android.content.res.Resources.getResourceEntryName(Resources.java:2127)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:433)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
        at android.view.View.performClick(View.java:4780)
        at android.widget.CompoundButton.performClick(CompoundButton.java:120)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5293)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
12-18 22:34:12.034 3720-3720/com.example.closeup I/Process: Sending signal. PID: 3720 SIG: 9

解决方案如下:

这是分段处理无线电组的解决方案。

1.从meat_entry_fragment.xml文件中的每个RadioButton删除所有行android:onClick="rbclick",并为每个RadioButton创建一个ID。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#aae4e4e4"
    android:orientation="horizontal">

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

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="1" />

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

        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="3" />

        <RadioButton
            android:id="@+id/radio4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="4" />

        <RadioButton
            android:id="@+id/radio5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="5" />

        <RadioButton
            android:id="@+id/radio6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="6" />
    </RadioGroup>
</LinearLayout>

2.在MealFragment类中创建OnCheckedChangeListener的实例。

private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.radio0:
                // Write your code here
                break;
            case R.id.radio1:
                // Write your code here
                break;
            case R.id.radio2:
                // Write your code here
                break;
            case R.id.radio3:
                // Write your code here
                break;
            case R.id.radio4:
                // Write your code here
                break;
            case R.id.radio5:
                // Write your code here
                break;
            case R.id.radio6:
                // Write your code here
                break;
            default:
                break;
        }
    }
};

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.meal_entry_fragment, container, false);
    rg = view.findViewById(R.id.group_no_1);
    rg.setOnCheckedChangeListener(onCheckedChangeListener);
    return view;
}