单击下一个按钮时如何检查是否选中了任何单选按钮?

时间:2017-08-29 03:42:43

标签: android

我正在开发一个测验游戏Android应用程序,其中包含带有收音机按钮的问题以及一个按钮(下一个)。因此,当按下按钮时,我必须检查用户是否从选项中选择了答案。如果没有那么警报应弹出一条消息,说明“您必须在继续下一个问题之前选择答案”。谁能帮帮我吗! Here is the design of my Quiz App

这是Java代码。

public class Main2Activity extends AppCompatActivity {
    Button button2;
    private TextView countLabel;
    private TextView questionLabel;
    private RadioButton answerBtn1;
    private RadioButton answerBtn2;
    private RadioButton answerBtn3;
    private RadioButton answerBtn4;
    private String rightAnswer;
    private int rightAnswerCount = 0;
    private int quizCount = 1;
    static final private int QUIZ_COUNT = 10;

    ArrayList<ArrayList<String>> quizArray = new ArrayList<>();

    String quizData[][] = {
            //{"Question", "Right Answer", "Choice1", "Choice2", "Choice3"}
            {"It is a stack of software for mobile devices which includes an Operating System, middleware and some key applications.", "Android", "Intent", "Toast", "Service"},
            {"Developed by Microsoft.", "Windows", "Android", "Blackberry", "iOS"},
            {"Used to build apps and run them directly on Apple devices.", "Xcode", "Android Studio", "Visual Studio", "Windows"},
            {"Represents a single screen with a user interface", "Activity", "Service", "Content Provider", "Broadcast receivers"},
            {"A component that runs in the background to perform operations or to perform work for remote processes", "Service", "Activity", "Content Provider", "Broadcast receivers"},
            {"Managers a shared set of app data", "Content Provider", "Activity", "Service", "Broadcast receiver"},
            {"Responds to system-wide Broadcast announcements", "Broadcast receivers", "Activity", "Content Provider", "Service"},
            {"It is connected to either the external world of application or internal world of application", "Intent", "Manifest File", "Resources", "Broadcast receiver"},
            {"It is a collection of views and other child views, it is an invisible part and the base class for layouts.", "viewGroup", "view", "Relative Layout", "Linear Layout"},
            {"It will show a pop up message on the surface of the window", "Toast Notification", "Status Bar Notification", "Dialogue Notification", "Action Bar"},
            {"It will show notifications on status bar", "Status Bar Notification", "Toast Notification", "Dialogue Notification", "Action Bar"},
            {"It is an activity related notification.", "Dialogue Notification", "Toast Notification", "Status Bar Notification", "Action Bar"},
            {"Contains code to test your application projects", "Test Module", "Library Module", "App Engine Module", "Android App Module"},
            {"Cannot be installed onto a device", "Library Module", "Test Module", "Android App Module", "App Engine Module"},
            {"Allows implementation of functionality such as Real-time interactions", "App Engine Module", "Test Module", "Library Module", "Android App Module"}
    };`enter code here`

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

        countLabel = (TextView) findViewById(R.id.countLabel);
        questionLabel = (TextView) findViewById(R.id.questionLabel);
        answerBtn1 = (RadioButton) findViewById(R.id.answerBtn1);
        answerBtn2 = (RadioButton) findViewById(R.id.answerBtn2);
        answerBtn3 = (RadioButton) findViewById(R.id.answerBtn3);
        answerBtn4 = (RadioButton) findViewById(R.id.answerBtn4);
        button2 = (Button) findViewById(R.id.button2);

        for (int i = 0; i < quizData.length; i++) {

            ArrayList<String> tmpArray = new ArrayList<>();
            tmpArray.add(quizData[i][0]);
            tmpArray.add(quizData[i][1]);
            tmpArray.add(quizData[i][2]);
            tmpArray.add(quizData[i][3]);
            tmpArray.add(quizData[i][4]);

            quizArray.add(tmpArray);
        }

        showNextQuiz();
    }

    public void showNextQuiz() {

        countLabel.setText("Question No." + "" + quizCount);

        Random random = new Random();
        int randomNum = random.nextInt(quizArray.size());

        ArrayList<String> quiz = quizArray.get(randomNum);

        questionLabel.setText(quiz.get(0));
        rightAnswer = quiz.get(1);

        quiz.remove(0);
        Collections.shuffle(quiz);

        answerBtn1.setText(quiz.get(0));
        answerBtn2.setText(quiz.get(1));
        answerBtn3.setText(quiz.get(2));
        answerBtn4.setText(quiz.get(3));

        answerBtn1.setChecked(false);
        answerBtn2.setChecked(false);
        answerBtn3.setChecked(false);
        answerBtn4.setChecked(false);

        quizArray.remove(randomNum);

    }
    public void checkAnswer(final View view) {


        button2.setOnClickListener(new View.OnClickListener() {
            final RadioButton answerBtn = (RadioButton) findViewById(view.getId());
            String btnText = answerBtn.getText().toString();

            private Boolean validationSuccess() {
                if(answerBtn.isChecked()==false){
                    alertDialog();
                    return false;
                }
                return true;
            }
            @Override
            public void onClick(View v) {
                if (btnText.equals(rightAnswer)) {

                    rightAnswerCount++;
                }else{
                    validationSuccess();
                }
                if (quizCount == QUIZ_COUNT) {

                    Intent intent = new Intent(getApplicationContext(), Main3Activity.class);
                    intent.putExtra("RIGHT_ANSWER_COUNT", rightAnswerCount);
                    startActivity(intent);

                } else {
                    quizCount++;
                    showNextQuiz();
                }
            }
        });

    }

    private void alertDialog() {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                Main2Activity.this);
        alertDialogBuilder.setMessage("Please ensure that you choose your answer!")
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });

        AlertDialog alert = alertDialogBuilder.create();
        alert.show();


    }

}

2 个答案:

答案 0 :(得分:2)

  

如果没有,则会弹出一条警告消息,说明“你必须   在继续下一个问题之前选择你的答案“

使用RadioGroupButtonradio button进行分组,并在按钮点击监听器中添加以下代码

if (radioGroup.getCheckedRadioButtonId() == -1)
{
  // no radio buttons are checked
  // You must choose your answer before proceeding to the next question
}
else
{
  // one of the radio buttons is checked
}

答案 1 :(得分:0)

您只需通过此方法检查单选按钮的状态

radioButton.isChecked()
相关问题