Cześć. Napisałem prosty program/quiz. Nie umiem poradzić sobie z przypisaniem odpowiedzi w taki sposób aby wyświetlała się on na przycisku.
Czyli załóżmy wyświetla się pytanie "pies czy kot" i pod pytaniem chciałbym dwa przyciski na lewym przycisku napis "kot", a na prawym napis "pies", potem kolejne pytanie i znowu dwa warianty odpowiedzi na przyciskach.
Jak najprościej to mogę uzyskać?
package com.example.quizzz;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private static final long COUNTDOWN_TIME = 150000;
private RadioButton[] answerRadioButtons = new RadioButton[2];
private TextView questionTextView;
private TextView timeTextView;
private String[] questions = {
"KOT czy PIES, czterej pancerni i...",
"UCHEM czy OKIEM, czym patrzymy?",
"PAWEŁ czy GAWEŁ, który z nich mieszkał na górze?"
};
private boolean[] answers = {
true,
true,
false
};
private List<Integer> randomQuestionIndexes = new ArrayList<>();
private int currentQuestionIndex = -1;
private int score = 0;
private CountDownTimer timer;
private boolean quizRunning = false;
private Button startButton;
private Button pauseButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
answerRadioButtons[0] = findViewById(R.id.answer_radio_button_1);
answerRadioButtons[1] = findViewById(R.id.answer_radio_button_2);
questionTextView = findViewById(R.id.question_text_view);
timeTextView = findViewById(R.id.time_text_view);
startButton = findViewById(R.id.start_button);
pauseButton = findViewById(R.id.pause_button);
answerRadioButtons[0].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkAnswer();
nextQuestion();
}
}
});
answerRadioButtons[1].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkAnswer();
nextQuestion();
}
}
});
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!quizRunning) {
startQuiz();
quizRunning = true;
}
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (quizRunning) {
timer.cancel();
quizRunning = false;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Quiz wstrzymany");
builder.setMessage("Kontynuować quiz?");
builder.setPositiveButton("Tak", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
timer.start();
quizRunning = true;
}
});
builder.setNegativeButton("Nie", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
endQuiz();
}
});
builder.setCancelable(false);
builder.show();
}
}
});
pauseButton.setEnabled(false);
}
private void startQuiz() {
score = 0;
currentQuestionIndex = -1;
// Randomly select 2 questions from the list without repetition
for (int i = 0; i < 21; i++) {
int randomIndex;
do {
randomIndex = new Random().nextInt(questions.length);
} while (randomQuestionIndexes.contains(randomIndex));
randomQuestionIndexes.add(randomIndex);
}
timer = new CountDownTimer(COUNTDOWN_TIME, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeTextView.setText("Czas: " + millisUntilFinished / 1000 + " s");
}
@Override
public void onFinish() {
endQuiz();
}
}.start();
nextQuestion();
startButton.setEnabled(false);
pauseButton.setEnabled(true);
}
private void nextQuestion() {
if (currentQuestionIndex == randomQuestionIndexes.size() - 1) {
endQuiz();
return;
}
currentQuestionIndex++;
questionTextView.setText(questions[randomQuestionIndexes.get(currentQuestionIndex)]);
answerRadioButtons[0].setChecked(false);
answerRadioButtons[1].setChecked(false);
}
private void checkAnswer() {
boolean selectedAnswer = answerRadioButtons[0].isChecked();
if (selectedAnswer == answers[randomQuestionIndexes.get(currentQuestionIndex)]) {
score++;
} else {
currentQuestionIndex = -1;
}
}
private void endQuiz() {
timer.cancel();
int totalQuestions = questions.length;
int correctAnswers = score;
int wrongAnswers = totalQuestions - correctAnswers;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Koniec czasu!");
builder.setMessage("Poprawne odpowiedzi: " + correctAnswers + "\nBłędne odpowiedzi: " + wrongAnswers);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.setCancelable(false);
builder.show();
}
public void startQuiz(View view) {
startQuiz();
}
public void pauseQuiz(View view) {
timer.cancel();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Quiz wstrzymany");
builder.setMessage("Kontynuować quiz?");
builder.setPositiveButton("Tak", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
timer.start();
}
});
builder.setNegativeButton("Nie", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
endQuiz();
}
});
builder.setCancelable(false);
builder.show();
}
}
github.com/k95nn/Quizzz