質問編集履歴

1

プログラム更新

2019/07/26 05:20

投稿

clond
clond

スコア6

test CHANGED
File without changes
test CHANGED
@@ -22,6 +22,162 @@
22
22
 
23
23
  ### 該当のソースコード
24
24
 
25
+ package com.example.quizdb;
26
+
27
+
28
+
29
+ import androidx.appcompat.app.AppCompatActivity;
30
+
31
+
32
+
33
+ import android.content.res.ColorStateList;
34
+
35
+ import android.graphics.Color;
36
+
37
+ import android.os.Bundle;
38
+
39
+ import android.view.View;
40
+
41
+ import android.widget.Button;
42
+
43
+ import android.widget.RadioButton;
44
+
45
+ import android.widget.RadioGroup;
46
+
47
+ import android.widget.TextView;
48
+
49
+ import android.widget.Toast;
50
+
51
+
52
+
53
+ import java.util.Collections;
54
+
55
+ import java.util.List;
56
+
57
+
58
+
59
+ public class QuizActivity extends AppCompatActivity {
60
+
61
+ private TextView textViewQuestion;
62
+
63
+ private TextView textViewScore;
64
+
65
+ private TextView textViewQuestionCount;
66
+
67
+ private TextView textViewCountDown;
68
+
69
+ private RadioGroup rbGroup;
70
+
71
+ private RadioButton rb1;
72
+
73
+ private RadioButton rb2;
74
+
75
+ private RadioButton rb3;
76
+
77
+ private Button buttonConfirmNext;
78
+
79
+
80
+
81
+ private ColorStateList textColorDefaultRb;
82
+
83
+
84
+
85
+ private List<Question> questionList;
86
+
87
+ private int questionCounter;
88
+
89
+ private int questionCountTotal;
90
+
91
+ private Question currentQuestion;
92
+
93
+
94
+
95
+ private int score;
96
+
97
+ private boolean answered;
98
+
99
+
100
+
101
+ @Override
102
+
103
+ protected void onCreate(Bundle savedInstanceState) {
104
+
105
+ super.onCreate(savedInstanceState);
106
+
107
+ setContentView(R.layout.activity_quiz);
108
+
109
+
110
+
111
+ textViewQuestion = findViewById(R.id.text_view_question);
112
+
113
+ textViewScore = findViewById(R.id.text_view_score);
114
+
115
+ textViewQuestionCount = findViewById(R.id.text_view_question_count);
116
+
117
+ textViewCountDown = findViewById(R.id.text_view_countdown);
118
+
119
+ rbGroup = findViewById(R.id.radio_group);
120
+
121
+ rb1 = findViewById(R.id.radio_button1);
122
+
123
+ rb2 = findViewById(R.id.radio_button2);
124
+
125
+ rb3 = findViewById(R.id.radio_button3);
126
+
127
+ buttonConfirmNext = findViewById(R.id.button_confirm_next);
128
+
129
+
130
+
131
+ textColorDefaultRb = rb1.getTextColors();
132
+
133
+
134
+
135
+ QuizDbHelper dbHelper = new QuizDbHelper(this);
136
+
137
+ questionList = dbHelper.getAllQuestions();
138
+
139
+ questionCountTotal = questionList.size();
140
+
141
+ Collections.shuffle(questionList);
142
+
143
+
144
+
145
+ showNextQuestion();
146
+
147
+
148
+
149
+ buttonConfirmNext.setOnClickListener(new View.OnClickListener() {
150
+
151
+ @Override
152
+
153
+ public void onClick(View v) {
154
+
155
+ if (!answered) {
156
+
157
+ if (rb1.isChecked() || rb2.isChecked() || rb3.isChecked()) {
158
+
159
+ checkAnswer();
160
+
161
+ } else {
162
+
163
+ Toast.makeText(QuizActivity.this, "Please select an answer", Toast.LENGTH_SHORT).show();
164
+
165
+ }
166
+
167
+ } else {
168
+
169
+ showNextQuestion();
170
+
171
+ }
172
+
173
+ }
174
+
175
+ });
176
+
177
+ }
178
+
179
+
180
+
25
181
  private void showNextQuestion() {
26
182
 
27
183
  rb1.setTextColor(textColorDefaultRb);
@@ -65,3 +221,95 @@
65
221
  }
66
222
 
67
223
  }
224
+
225
+
226
+
227
+ private void checkAnswer() {
228
+
229
+ answered = true;
230
+
231
+
232
+
233
+ RadioButton rbSelected = findViewById(rbGroup.getCheckedRadioButtonId());
234
+
235
+ int answerNr = rbGroup.indexOfChild(rbSelected) + 1;
236
+
237
+
238
+
239
+ if (answerNr == currentQuestion.getAnswerNr()) {
240
+
241
+ score++;
242
+
243
+ textViewScore.setText("Score: " + score);
244
+
245
+ }
246
+
247
+
248
+
249
+ showSolution();
250
+
251
+ }
252
+
253
+
254
+
255
+ private void showSolution() {
256
+
257
+ rb1.setTextColor(Color.RED);
258
+
259
+ rb2.setTextColor(Color.RED);
260
+
261
+ rb3.setTextColor(Color.RED);
262
+
263
+
264
+
265
+ switch (currentQuestion.getAnswerNr()) {
266
+
267
+ case 1:
268
+
269
+ rb1.setTextColor(Color.GREEN);
270
+
271
+ textViewQuestion.setText("Answer 1 is correct");
272
+
273
+ break;
274
+
275
+ case 2:
276
+
277
+ rb2.setTextColor(Color.GREEN);
278
+
279
+ textViewQuestion.setText("Answer 2 is correct");
280
+
281
+ break;
282
+
283
+ case 3:
284
+
285
+ rb3.setTextColor(Color.GREEN);
286
+
287
+ textViewQuestion.setText("Answer 3 is correct");
288
+
289
+ break;
290
+
291
+ }
292
+
293
+
294
+
295
+ if (questionCounter < questionCountTotal) {
296
+
297
+ buttonConfirmNext.setText("Next");
298
+
299
+ } else {
300
+
301
+ buttonConfirmNext.setText("Finish");
302
+
303
+ }
304
+
305
+ }
306
+
307
+
308
+
309
+ private void finishQuiz() {
310
+
311
+ finish();
312
+
313
+ }
314
+
315
+ }