简体   繁体   English

如何检查按下按钮时选中了哪些单选按钮

[英]How to check which radio buttons are checked upon pressing a button

I'm getting this error when I click on the button that executes a method.单击执行方法的按钮时出现此错误。

Process: com.example.myapplication, PID: 7757
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
    at android.view.View.performClick(View.java:6304)
    at android.view.View$PerformClick.run(View.java:24803)
    at android.os.Handler.handleCallback(Handler.java:794)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:6635)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:6304) 
    at android.view.View$PerformClick.run(View.java:24803) 
    at android.os.Handler.handleCallback(Handler.java:794) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:176) 
    at android.app.ActivityThread.main(ActivityThread.java:6635) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
 Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.RadioButton
    at com.example.myapplication.MockTest.submitbtn(MockTest.java:66)
    at java.lang.reflect.Method.invoke(Native Method) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
    at android.view.View.performClick(View.java:6304) 
    at android.view.View$PerformClick.run(View.java:24803) 
    at android.os.Handler.handleCallback(Handler.java:794) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:176) 
    at android.app.ActivityThread.main(ActivityThread.java:6635) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 

The method is called upon by android:onClick through xml upon clicking the button.单击按钮时,该方法由 android:onClick 通过 xml 调用。

This is the method,这是方法,

public void submitbtn(View view)
    {
        boolean checked = ((RadioButton) view).isChecked();
        switch (view.getId()) {
            case R.id.q1r1: if (checked)
                score++;
                break;
            case R.id.q2r4: if (checked)
                score++;
                break;
            case R.id.q3r4: if (checked)
                score++;
                break;
            case R.id.q4r2: if (checked)
                score++;
                break;
        }
    }

this is the xml entry for the button,这是按钮的 xml 条目,

<Button
            android:id="@+id/submitbtn"
            android:layout_width="100dp"
            android:layout_height="65dp"
            android:layout_marginStart="150dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="150dp"
            android:text="Submit"
            android:onClick="submitbtn"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/q4r" />

I looked at the error and noticed that it says it's caused because radiobutton widget cannot be cast to button widget, is that right?我查看了错误并注意到它说它是由于 radiobutton 小部件无法转换为按钮小部件引起的,对吗? if so how can I implement this.如果是这样,我该如何实施。 What I'm trying to do is simply check the radio button's current state(checked or not) once the button is clicked.我想要做的只是在单击按钮后检查单选按钮的当前状态(已选中或未选中)。 My previous approach, which worked, was this exact method but called from a radiobutton.我以前的方法有效,就是这种确切的方法,但从单选按钮调用。 I had put an android:onClick on the radiobutton's xml file.我在单选按钮的 xml 文件上放了一个 android:onClick 。 But this somehow does not work.但这不知何故不起作用。

Found the solution, apparently I need to use radiogroups instead of directly using radiobuttons, the only change I made is in the code, here it is,找到了解决方案,显然我需要使用 radiogroups 而不是直接使用 radiobuttons,我所做的唯一更改是在代码中,这里是,

public void submitbtn(View view)
    {
         RadioGroup q4r = findViewById(R.id.q4r);
        //Checking which radio button is checked, if any, and then executing tasks
        if(q1r.getCheckedRadioButtonId() == R.id.q1r1)
        {
            //execute tasks
        }
        else if(q1r.getCheckedRadioButtonId() == -1)
        {
            //Nothing
        }
        else
        {
            //execute tasks
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM