【问题标题】:Handling Radio Group and Button in Fragment处理片段中的单选组和按钮
【发布时间】:2020-04-11 06:42:44
【问题描述】:

首先看一下我的片段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 函数)可以处理 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

【问题讨论】:

  • 当您尝试为不在Activity 中的方法设置onClick 属性时,您总是会收到错误消息。您不应该真正将onClickRadioButtons 一起使用。相反,请在代码中的 RadioGroup 上设置 OnCheckedChangeListener

标签: java android radio-button android-radiogroup


【解决方案1】:

public void rbclick(View v) 中的参数 View v 是您要查找的 RadioButton

【讨论】:

    【解决方案2】:

    尝试使用以下代码:

    public void rbclick (View v){
    
                int radiobtnid=rg.getCheckedRadioButtonId();
                rb =(RadioButton)v.findViewById(radiobtnid);
    
            }
    

    【讨论】:

    • 当我点击单选按钮时应用程序仍然崩溃:(
    【解决方案3】:

    这是在片段中处理无线电组的解决方案。

    1.从meal_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;
    }
    

    【讨论】:

    • 我已经用另一种方法解决了这个问题。无论如何,这很好用也感谢@Nhat Giang :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多