回答編集履歴
2
修正
test
CHANGED
@@ -98,7 +98,7 @@
|
|
98
98
|
|
99
99
|
|
100
100
|
|
101
|
-
/解答Buttonに個別にしていた設定をListとforを使ってまとめて記述
|
101
|
+
//解答Buttonに個別にしていた設定をListとforを使ってまとめて記述
|
102
102
|
|
103
103
|
for(int i = 0; i < answers.size(); i++){
|
104
104
|
|
@@ -128,10 +128,16 @@
|
|
128
128
|
|
129
129
|
public void onClick(View view){
|
130
130
|
|
131
|
+
//クリックされたのが解答ボタンのどれかだった場合は解答の判定
|
132
|
+
|
131
133
|
if(buttons.contains(view))
|
132
134
|
|
133
135
|
checkAnswer(view);
|
134
136
|
|
137
|
+
|
138
|
+
|
139
|
+
//nextButtonがクリックされたら次の問題への処理
|
140
|
+
|
135
141
|
else if(view == nextButton)
|
136
142
|
|
137
143
|
goNext(view);
|
@@ -178,7 +184,9 @@
|
|
178
184
|
|
179
185
|
public void goNext(View view) {
|
180
186
|
|
187
|
+
//「10問目まで」ならここを10に書き換えればよい
|
188
|
+
|
181
|
-
if (currentQuiz ==
|
189
|
+
if (currentQuiz == 10) {
|
182
190
|
|
183
191
|
// show result
|
184
192
|
|
1
コードを実際に書いてみた
test
CHANGED
@@ -7,3 +7,237 @@
|
|
7
7
|
- 現状の問題点
|
8
8
|
|
9
9
|
各ButtonにOnClickLisnerをセットしていないため,現段階ではどのButtonを押しても何も起こらない
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
ある程度コードを書いてみます.
|
14
|
+
|
15
|
+
```java
|
16
|
+
|
17
|
+
//クリックリスナーを実装
|
18
|
+
|
19
|
+
public class MyQuiz extends ActionBarActivity implements View.OnClickListener{
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
public final static String EXTRA_MYSCORE = "com.example.taguchi.myquizapp.MYSCORE";
|
24
|
+
|
25
|
+
private ArrayList<String[]> quizSet = new ArrayList<String[]>();
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
private TextView scoreText;
|
30
|
+
|
31
|
+
private TextView qText;
|
32
|
+
|
33
|
+
private Button a0Button, a1Button, a2Button;
|
34
|
+
|
35
|
+
private List<Button> buttons; //Buttonごとの操作をまとめてするためリスト化
|
36
|
+
|
37
|
+
private Button nextButton;
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
private int currentQuiz = 0;
|
42
|
+
|
43
|
+
private int score = 0;
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
@Override
|
48
|
+
|
49
|
+
protected void onCreate(Bundle savedInstanceState) {
|
50
|
+
|
51
|
+
super.onCreate(savedInstanceState);
|
52
|
+
|
53
|
+
setContentView(R.layout.activity_my_quiz);
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
loadQuizSet();
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
Collections.shuffle(quizSet); //問題セットはこのタイミングで1度だけシャッフルする
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
getViews();
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
setQuiz();
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
private void showScore() {
|
76
|
+
|
77
|
+
scoreText.setText("Score: " + score + " / " + quizSet.size());
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
private void setQuiz() {
|
84
|
+
|
85
|
+
qText.setText(quizSet.get(currentQuiz)[0]);
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
ArrayList<String> answers = new ArrayList<String>();
|
90
|
+
|
91
|
+
for (int i = 1; i <= 3; i++) {
|
92
|
+
|
93
|
+
answers.add(quizSet.get(currentQuiz)[i]);
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
Collections.shuffle(answers);
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
/解答Buttonに個別にしていた設定をListとforを使ってまとめて記述
|
102
|
+
|
103
|
+
for(int i = 0; i < answers.size(); i++){
|
104
|
+
|
105
|
+
Button button = buttons.get(i);
|
106
|
+
|
107
|
+
button.setText(answers.get(i));
|
108
|
+
|
109
|
+
button.setEnabled(true);
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
nextButton.setEnabled(false);
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
showScore();
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
//ボタンクリック時の動作を実装
|
126
|
+
|
127
|
+
@Override
|
128
|
+
|
129
|
+
public void onClick(View view){
|
130
|
+
|
131
|
+
if(buttons.contains(view))
|
132
|
+
|
133
|
+
checkAnswer(view);
|
134
|
+
|
135
|
+
else if(view == nextButton)
|
136
|
+
|
137
|
+
goNext(view);
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
public void checkAnswer(View view) {
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
//判定に関してはそのままでいい
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
// ここの操作もforで書き換え
|
152
|
+
|
153
|
+
for(Button button : buttons){
|
154
|
+
|
155
|
+
button.setEnabled(false);
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
nextButton.setEnabled(true);
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
// next quiz
|
164
|
+
|
165
|
+
currentQuiz++;
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
if (currentQuiz == quizSet.size()) {
|
170
|
+
|
171
|
+
nextButton.setText("Check result");
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
public void goNext(View view) {
|
180
|
+
|
181
|
+
if (currentQuiz == quizSet.size()) {
|
182
|
+
|
183
|
+
// show result
|
184
|
+
|
185
|
+
Intent intent = new Intent(this, MyResult.class);
|
186
|
+
|
187
|
+
intent.putExtra(EXTRA_MYSCORE, score + " / " + quizSet.size());
|
188
|
+
|
189
|
+
startActivity(intent);
|
190
|
+
|
191
|
+
} else {
|
192
|
+
|
193
|
+
setQuiz();
|
194
|
+
|
195
|
+
}
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
@Override
|
202
|
+
|
203
|
+
public void onResume() {
|
204
|
+
|
205
|
+
//変更不要なので省略
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
private void getViews() {
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
//各View,Buttonを取得する記述(もとと同じでいいので省略)
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
buttons = Arrays.asList(a0Button, a1Button, a2Buttons); //Buttonをリスト化
|
220
|
+
|
221
|
+
//各Buttonにクリックリスナを設定
|
222
|
+
|
223
|
+
for(Button button : buttons){
|
224
|
+
|
225
|
+
button.setOnClickListener(this);
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
nextButton.setOnClickListener(this);
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
private void loadQuizSet() {
|
236
|
+
|
237
|
+
//ここは変更しなくていいので省略
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
```
|