質問編集履歴
1
プログラム更新
title
CHANGED
File without changes
|
body
CHANGED
@@ -10,6 +10,84 @@
|
|
10
10
|
```
|
11
11
|
|
12
12
|
### 該当のソースコード
|
13
|
+
package com.example.quizdb;
|
14
|
+
|
15
|
+
import androidx.appcompat.app.AppCompatActivity;
|
16
|
+
|
17
|
+
import android.content.res.ColorStateList;
|
18
|
+
import android.graphics.Color;
|
19
|
+
import android.os.Bundle;
|
20
|
+
import android.view.View;
|
21
|
+
import android.widget.Button;
|
22
|
+
import android.widget.RadioButton;
|
23
|
+
import android.widget.RadioGroup;
|
24
|
+
import android.widget.TextView;
|
25
|
+
import android.widget.Toast;
|
26
|
+
|
27
|
+
import java.util.Collections;
|
28
|
+
import java.util.List;
|
29
|
+
|
30
|
+
public class QuizActivity extends AppCompatActivity {
|
31
|
+
private TextView textViewQuestion;
|
32
|
+
private TextView textViewScore;
|
33
|
+
private TextView textViewQuestionCount;
|
34
|
+
private TextView textViewCountDown;
|
35
|
+
private RadioGroup rbGroup;
|
36
|
+
private RadioButton rb1;
|
37
|
+
private RadioButton rb2;
|
38
|
+
private RadioButton rb3;
|
39
|
+
private Button buttonConfirmNext;
|
40
|
+
|
41
|
+
private ColorStateList textColorDefaultRb;
|
42
|
+
|
43
|
+
private List<Question> questionList;
|
44
|
+
private int questionCounter;
|
45
|
+
private int questionCountTotal;
|
46
|
+
private Question currentQuestion;
|
47
|
+
|
48
|
+
private int score;
|
49
|
+
private boolean answered;
|
50
|
+
|
51
|
+
@Override
|
52
|
+
protected void onCreate(Bundle savedInstanceState) {
|
53
|
+
super.onCreate(savedInstanceState);
|
54
|
+
setContentView(R.layout.activity_quiz);
|
55
|
+
|
56
|
+
textViewQuestion = findViewById(R.id.text_view_question);
|
57
|
+
textViewScore = findViewById(R.id.text_view_score);
|
58
|
+
textViewQuestionCount = findViewById(R.id.text_view_question_count);
|
59
|
+
textViewCountDown = findViewById(R.id.text_view_countdown);
|
60
|
+
rbGroup = findViewById(R.id.radio_group);
|
61
|
+
rb1 = findViewById(R.id.radio_button1);
|
62
|
+
rb2 = findViewById(R.id.radio_button2);
|
63
|
+
rb3 = findViewById(R.id.radio_button3);
|
64
|
+
buttonConfirmNext = findViewById(R.id.button_confirm_next);
|
65
|
+
|
66
|
+
textColorDefaultRb = rb1.getTextColors();
|
67
|
+
|
68
|
+
QuizDbHelper dbHelper = new QuizDbHelper(this);
|
69
|
+
questionList = dbHelper.getAllQuestions();
|
70
|
+
questionCountTotal = questionList.size();
|
71
|
+
Collections.shuffle(questionList);
|
72
|
+
|
73
|
+
showNextQuestion();
|
74
|
+
|
75
|
+
buttonConfirmNext.setOnClickListener(new View.OnClickListener() {
|
76
|
+
@Override
|
77
|
+
public void onClick(View v) {
|
78
|
+
if (!answered) {
|
79
|
+
if (rb1.isChecked() || rb2.isChecked() || rb3.isChecked()) {
|
80
|
+
checkAnswer();
|
81
|
+
} else {
|
82
|
+
Toast.makeText(QuizActivity.this, "Please select an answer", Toast.LENGTH_SHORT).show();
|
83
|
+
}
|
84
|
+
} else {
|
85
|
+
showNextQuestion();
|
86
|
+
}
|
87
|
+
}
|
88
|
+
});
|
89
|
+
}
|
90
|
+
|
13
91
|
private void showNextQuestion() {
|
14
92
|
rb1.setTextColor(textColorDefaultRb);
|
15
93
|
rb2.setTextColor(textColorDefaultRb);
|
@@ -31,4 +109,50 @@
|
|
31
109
|
} else {
|
32
110
|
finishQuiz();
|
33
111
|
}
|
34
|
-
}
|
112
|
+
}
|
113
|
+
|
114
|
+
private void checkAnswer() {
|
115
|
+
answered = true;
|
116
|
+
|
117
|
+
RadioButton rbSelected = findViewById(rbGroup.getCheckedRadioButtonId());
|
118
|
+
int answerNr = rbGroup.indexOfChild(rbSelected) + 1;
|
119
|
+
|
120
|
+
if (answerNr == currentQuestion.getAnswerNr()) {
|
121
|
+
score++;
|
122
|
+
textViewScore.setText("Score: " + score);
|
123
|
+
}
|
124
|
+
|
125
|
+
showSolution();
|
126
|
+
}
|
127
|
+
|
128
|
+
private void showSolution() {
|
129
|
+
rb1.setTextColor(Color.RED);
|
130
|
+
rb2.setTextColor(Color.RED);
|
131
|
+
rb3.setTextColor(Color.RED);
|
132
|
+
|
133
|
+
switch (currentQuestion.getAnswerNr()) {
|
134
|
+
case 1:
|
135
|
+
rb1.setTextColor(Color.GREEN);
|
136
|
+
textViewQuestion.setText("Answer 1 is correct");
|
137
|
+
break;
|
138
|
+
case 2:
|
139
|
+
rb2.setTextColor(Color.GREEN);
|
140
|
+
textViewQuestion.setText("Answer 2 is correct");
|
141
|
+
break;
|
142
|
+
case 3:
|
143
|
+
rb3.setTextColor(Color.GREEN);
|
144
|
+
textViewQuestion.setText("Answer 3 is correct");
|
145
|
+
break;
|
146
|
+
}
|
147
|
+
|
148
|
+
if (questionCounter < questionCountTotal) {
|
149
|
+
buttonConfirmNext.setText("Next");
|
150
|
+
} else {
|
151
|
+
buttonConfirmNext.setText("Finish");
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
private void finishQuiz() {
|
156
|
+
finish();
|
157
|
+
}
|
158
|
+
}
|