检查是否在Android中检查了任何RadioButton

时间:2018-07-22 10:31:25

标签: android

我想检查是否选中了哪个RadioButton以及选中了哪个。 我有

package promedica.test_osobowosci;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;

public class Question1 extends AppCompatActivity {

    private Button nextButton;
    private Button stopButton;
    private RadioButton answer1;
    private RadioButton answer2;
    private RadioButton answer3;
    private RadioButton answer4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question1);
        setTitle("Pytanie 1 z 10");

        answer1 = findViewById(R.id.radioButtonQ1_1);
        answer1 = findViewById(R.id.radioButtonQ1_2);
        answer1 = findViewById(R.id.radioButtonQ1_3);
        answer1 = findViewById(R.id.radioButtonQ1_4);

        nextButton = findViewById(R.id.nextTo2Button);
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isAnyRadioButtonChecke();
            }
        });

        stopButton = findViewById(R.id.stopButton1);
        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openMainActivity();
            }
        });
    }

    public void openQuestion2Activity(){
        Intent intent = new Intent(this, Question2.class);
        startActivity(intent);
    }

    public void openMainActivity(){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
    }

    public void noAnswerCheckedAlert() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Nie zaznaczono odpowiedzi!");
        alertDialogBuilder.setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(Question1.this, "Zaznacz odpowiedź", Toast.LENGTH_LONG).show();
                    }
                });
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

    public void isAnyRadioButtonChecked(){
        if(answer1.isChecked() || answer2.isChecked() || answer3.isChecked() || answer4.isChecked())
            openQuestion2Activity();
        else
            noAnswerCheckedAlert();
    }

}

我收到错误消息:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.RadioButton.isChecked()' on a null object reference
        at promedica.test_osobowosci.Question1.isAnyRadioButtonChecked(Question1.java:76)
        at promedica.test_osobowosci.Question1$1.onClick(Question1.java:38)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

当我单击带有检查的nextButton时,没有单击任何RadioButton。

但是我单击任何RadioButton,然后单击nextButton,应用程序始终显示警报。我尝试在OnClickListener按钮方法中声明RadioButtons,但无法解决我的问题。 有什么问题吗?

编辑:

activity_question1。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Question1">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="false"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="85dp"
        android:text="Question1"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout_editor_absoluteY="25dp" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="19dp"
        android:layout_marginTop="0dp"
        android:scaleType="fitXY"
        android:cropToPadding="false"
        app:srcCompat="@mipmap/obrot" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="19dp"
        android:layout_marginTop="0dp"
        android:scaleType="fitXY"
        android:cropToPadding="false"
        app:srcCompat="@mipmap/obrot" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="159dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="109dp"
        android:layout_marginLeft="85dp">


        <RadioButton
            android:id="@+id/radioButtonQ1_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Answer1"
            android:inputType="textMultiLine"
            android:layout_marginRight="50dp"
            android:theme="@style/MyRadioButton" />

        <RadioButton
            android:id="@+id/radioButtonQ1_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Answer2"
            android:inputType="textMultiLine"
            android:layout_marginRight="50dp"
            android:theme="@style/MyRadioButton" />

        <RadioButton
            android:id="@+id/radioButtonQ1_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Answer3"
            android:inputType="textMultiLine"
            android:layout_marginRight="50dp"
            android:theme="@style/MyRadioButton" />

        <RadioButton
            android:id="@+id/radioButtonQ1_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="false"
            android:inputType="textMultiLine"
            android:text="Answer4"
            android:layout_marginRight="50dp"
            android:theme="@style/MyRadioButton" />
    </RadioGroup>

    <Button
        android:id="@+id/nextTo2Button"
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:layout_marginTop="305dp"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="161dp"
        android:background="@drawable/custom_button"
        android:text="Next"
        android:textAllCaps="false"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/stopButton1"
        android:layout_width="200dp"
        android:layout_height="75dp"
        android:layout_marginTop="395dp"
        android:layout_centerHorizontal="true"
        android:background="@drawable/custom_button_blue"
        android:text="Stop"
        android:textAllCaps="false"
        android:textColor="#ffffff" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="60dp"
        android:layout_alignParentStart="true"
        app:srcCompat="@mipmap/oie_transparent" />

</RelativeLayout>

4 个答案:

答案 0 :(得分:0)

您可以在xml中使用RadioGroup

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radiobuttongroup"       
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton android:id="@+id/radio_q1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/option1"
        android:onClick="onRadioButtonClicked"/>
    <RadioButton android:id="@+id/radio_q2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/options2"
        android:onClick="onRadioButtonClicked"/>
</RadioGroup>

和活动文件

RadioGroup radioGroup;

radioGroup = (RadioGroup) findViewById(R.id.radiobuttongroup);
final String[] checkedOption = new String[1];

radioGroup.setOnCheckedChangeListener(new 
RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.radio_q1:
                checkedOption[0] = 1;
                break;
             case R.id.radio_q2:
                checkedOption[0] = 2;
                break;
                .
                .
                .
        }
      }
    });

答案 1 :(得分:0)

执行以下操作,而不是检查单个单选按钮:

   if (radioGroup.getCheckedRadioButtonId() == -1)
{
        noAnswerCheckedAlert();
}
else
{
        openQuestion2Activity();
}

答案 2 :(得分:0)

   <RadioGroup
        android:id="@+id/radio"
        android:layout_width="match_parent"
        android:layout_height="159dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="109dp"
        android:layout_marginLeft="85dp">


        <RadioButton
            android:id="@+id/radioButtonQ1_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Answer1"
            android:inputType="textMultiLine"
            android:layout_marginRight="50dp"
            android:theme="@style/MyRadioButton" />

        <RadioButton
            android:id="@+id/radioButtonQ1_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Answer2"
            android:inputType="textMultiLine"
            android:layout_marginRight="50dp"
            android:theme="@style/MyRadioButton" />

        <RadioButton
            android:id="@+id/radioButtonQ1_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Answer3"
            android:inputType="textMultiLine"
            android:layout_marginRight="50dp"
            android:theme="@style/MyRadioButton" />

        <RadioButton
            android:id="@+id/radioButtonQ1_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="false"
            android:inputType="textMultiLine"
            android:text="Answer4"
            android:layout_marginRight="50dp"
            android:theme="@style/MyRadioButton" />
    </RadioGroup>    

JAVA代码:

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio);
    nextButton = findViewById(R.id.nextTo2Button);
    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (radioGroup.getCheckedRadioButtonId() == -1) {
                // no radio buttons are checked
                noAnswerCheckedAlert();
            } else {
                // one of the radio buttons is checked
                openQuestion2Activity();
            }
        }
    });

答案 3 :(得分:0)

使用RadioGroup来检查是否未选中RadioButton:

RadioGroup radioGroup;
....
if(radioGroup.getCheckedRadioButtonId() != answer1 || ........)
{
  AlertDialog...
}
else
{
  openQuestion2Activity(); 
}