如何在Android中通过单选按钮正确使用“ if / else if”

时间:2019-07-28 12:40:19

标签: java android

我正在尝试通过使用单选按钮和if语句为选择题提供正确答案的简单检查。

我想将其用作教学应用程序的一部分,但是我可能会误解

的用法
int radioId = radioGroup.getCheckedRadioButtonId()

我希望你们能帮助我。我正在单选组中使用四个单选按钮,并在下面发布了xml和java。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".IntroQuiz1">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="68dp"
        android:layout_marginEnd="8dp"
        android:text="Quiz1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="56dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="checkButton"
            android:text="RadioButton1" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="checkButton"
            android:text="RadioButton2" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="checkButton"
            android:text="RadioButton3" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="checkButton"
            android:text="RadioButton4" />

    </RadioGroup>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="140dp"
        android:layout_marginEnd="156dp"
        android:text=""
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.906"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

    <Button
        android:id="@+id/button_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="52dp"
        android:text="Check"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

public class IntroQuiz1 extends AppCompatActivity {

    RadioGroup radioGroup;
    RadioButton radioButton;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intro_quiz1);

        radioGroup = findViewById(R.id.radioGroup);
        textView = findViewById(R.id.textView2);


        Button buttonCheck = findViewById(R.id.button_check);
        buttonCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int radioId = radioGroup.getCheckedRadioButtonId();
                int test = 1;

                radioButton = findViewById(radioId);
                String checkString = (String) radioButton.getText();

                if (checkString == "RadioButton1") {
                    textView.setText("Korrekt");
                } else {
                    textView.setText(checkString);
                }

            }
        });
    }

    public void checkButton(View v) {
        int radioId = radioGroup.getCheckedRadioButtonId();

        radioButton = findViewById(radioId);

        Toast.makeText(this, "Selected Radio Button: " + radioButton.getText(),
                Toast.LENGTH_SHORT).show();

    }
}

但是,我没有选中任何单选按钮来打印“ Korrekt”。这已经是一种解决方法。我不想比较字符串。我宁愿使用Id,但是当我尝试使用radioId检查正确答案时,radioId似乎不是一个整数,正如我从到目前为止所学到的那样,这是正确的吗? 因此,我有点困惑,有人可以帮我如何更正此代码并告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:2)

您应该将自己选择的ID与其他(而非字符串)进行比较; 喜欢:

 int radioId = radioGroup.getCheckedRadioButtonId();
    switch(radioId ){
        case R.id.radioButton1:
            //your code;
            break;
        case R.id.radioButton2:
            //your code;
            break;
        case R.id.radioButton3:
            //your code;
            break;
    }