質問編集履歴

10

誤字

2019/12/26 16:13

投稿

kubonos
kubonos

スコア6

test CHANGED
File without changes
test CHANGED
@@ -62,7 +62,7 @@
62
62
 
63
63
  //IDをストリングで取得・・・ここがうまくいきません
64
64
 
65
- String btnId = answerBtn.getText().toString();としてみたのですが
65
+ String btnId = answerBtn.getId().toString();としてみたのですが
66
66
 
67
67
  //エラー: intは間接参照できませんというエラーが出ています。
68
68
 
@@ -430,7 +430,7 @@
430
430
 
431
431
  //ボタンのIdをテキストで取得・・・ここが分かりません
432
432
 
433
- String btnId = answerBtn.getText().toString(); //エラー: intは間接参照できませんというエラーが
433
+ String btnId = answerBtn.getId().toString(); //エラー: intは間接参照できませんというエラーが
434
434
 
435
435
 
436
436
 

9

修正

2019/12/26 16:13

投稿

kubonos
kubonos

スコア6

test CHANGED
File without changes
test CHANGED
@@ -1,22 +1,14 @@
1
+ android studioでクイズゲーム作ってます。
2
+
3
+
4
+
1
- xmlで
5
+ xmlで設置した
2
6
 
3
7
 
4
8
 
5
9
  <Button
6
10
 
7
- android:id="@+id/answerBtn1"
11
+ android:id="@+id/answerBtn1"/>
8
-
9
- android:layout_width="90dp"
10
-
11
- android:layout_height="55dp"
12
-
13
- android:onClick="checkAnswer"
14
-
15
- android:text="A"
16
-
17
- tools:layout_editor_absoluteX="37dp"
18
-
19
- tools:layout_editor_absoluteY="284dp" />
20
12
 
21
13
 
22
14
 

8

修正

2019/12/26 16:03

投稿

kubonos
kubonos

スコア6

test CHANGED
File without changes
test CHANGED
@@ -72,7 +72,7 @@
72
72
 
73
73
  String btnId = answerBtn.getText().toString();としてみたのですが
74
74
 
75
- //intString型に変換できない!というエラーが出ています。
75
+ //エラー: intは間接参照できませんというエラーが出ています。
76
76
 
77
77
 
78
78
 
@@ -520,9 +520,17 @@
520
520
 
521
521
 
522
522
 
523
-
523
+ android:id="@+id/answerBtn1"
524
+
524
-
525
+ のanswerBtn1をString型で参照できれば、嬉しいです。
526
+
527
+
528
+
525
-
529
+ String btnId = answerBtn.getText().toString();  //エラー: intは間接参照できません
530
+
531
+
532
+
533
+ ここの部分の、idをjavaでString型で参照する、書き方を知りたいです。
526
534
 
527
535
 
528
536
 

7

修正

2019/12/26 15:57

投稿

kubonos
kubonos

スコア6

test CHANGED
File without changes
test CHANGED
@@ -234,10 +234,54 @@
234
234
 
235
235
 
236
236
 
237
- //private TextView questionLabel;
237
+
238
238
 
239
239
  private Button answerBtn1;
240
240
 
241
+ private Button answerBtn2;
242
+
243
+ private Button answerBtn3;
244
+
245
+ private Button answerBtn4;
246
+
247
+ private Button answerBtn5;
248
+
249
+ private Button answerBtn6;
250
+
251
+ private Button answerBtn7;
252
+
253
+ private Button answerBtn8;
254
+
255
+ private Button answerBtn9;
256
+
257
+ private Button answerBtn10;
258
+
259
+ private Button answerBtn11;
260
+
261
+ private Button answerBtn12;
262
+
263
+
264
+
265
+ private String rightAnswer; //String文字型であることに注目したい
266
+
267
+ private int rightAnswerCount = 0; //初期化=定義
268
+
269
+ private int quizCount = 1; //初期化=定義
270
+
271
+ static final private int QUIZ_COUNT = 50; //全部の出題数を入れるのだ!!
272
+
273
+
274
+
275
+ private int randomNum; //これで{}をまたげる
276
+
277
+ //private int randomNum;は死ぬほど重要です
278
+
279
+
280
+
281
+
282
+
283
+
284
+
241
285
 
242
286
 
243
287
 
@@ -248,23 +292,23 @@
248
292
 
249
293
  String quizData[][] = {
250
294
 
251
- //{"画像名","正解"},
295
+ //{"画像名","正解"},
252
-
296
+
253
- //,←これ[,]が必要なんだ!!!
297
+ //,←これ[,]が必要なんだ!!!
254
-
298
+
255
- {"a","b"},
299
+ {"a","a"},
256
300
 
257
301
  {"b","b"},
258
302
 
259
303
 
260
304
 
261
-
262
-
263
305
  };
264
306
 
265
307
 
266
308
 
267
-
309
+ MySound mySound = new MySound(); // ① サウンド準備
310
+
311
+
268
312
 
269
313
 
270
314
 
@@ -278,7 +322,29 @@
278
322
 
279
323
 
280
324
 
325
+ // ② 効果音の準備wavだけどmp3sで定義されている。クイズデータと同じ並びで並べる
326
+
327
+ int[] mp3s = {
328
+
281
- soundPlayer = new SoundPlayer(this);
329
+ R.raw.a, //[0]
330
+
331
+ R.raw.b, //[1]
332
+
333
+ R.raw.c, //[2]
334
+
335
+
336
+
337
+ };
338
+
339
+ //ここに音声を、クイズと全く同じ順番で並べましょう!!!!!!【追加していく2/2】
340
+
341
+
342
+
343
+ mySound.onCreate(this, mp3s); // ③ 初期化、言葉がまぎらわしいがただ定義(代入)しただけ
344
+
345
+
346
+
347
+
282
348
 
283
349
  questionImage = findViewById(R.id.questionImage);
284
350
 
@@ -286,27 +352,23 @@
286
352
 
287
353
 
288
354
 
289
-
290
-
291
- //quizDataからクイズ出題用のquizArrayを作成する
355
+ //quizDataからクイズ出題用のquizArrayを作成する
292
356
 
293
357
  for (int i = 0; i < quizData.length; i++) {
294
358
 
295
359
 
296
360
 
297
- //新しいArrayListを用意
361
+ //新しいArrayListを用意
298
362
 
299
363
  ArrayList<String> tmpArray = new ArrayList<>();
300
364
 
301
- //クイズデータを追加
365
+ //クイズデータを追加
302
366
 
303
367
  tmpArray.add(quizData[i][0]); //画像名
304
368
 
305
369
 
306
370
 
307
-
308
-
309
- //tmpArrayをquizArrayに追加する
371
+ //tmpArrayをquizArrayに追加する
310
372
 
311
373
  quizArray.add(tmpArray);
312
374
 
@@ -324,13 +386,13 @@
324
386
 
325
387
  public void showNextQuiz() {
326
388
 
327
- //クイズカウントラベルを更新
389
+ //クイズカウントラベルを更新
328
390
 
329
391
  countLabel.setText("Q" + quizCount);
330
392
 
331
393
 
332
394
 
333
- //ランダムな数字を取得
395
+ //ランダムな数字を取得
334
396
 
335
397
  Random random = new Random();
336
398
 
@@ -338,13 +400,13 @@
338
400
 
339
401
 
340
402
 
341
- //randomNumを使ってquizArrayからクイズを一つ取り出す
403
+ //randomNumを使ってquizArrayからクイズを一つ取り出す
342
404
 
343
405
  ArrayList<String> quiz = quizArray.get(randomNum);
344
406
 
345
407
 
346
408
 
347
- //画像をセットする
409
+ //画像をセットする
348
410
 
349
411
  questionImage.setImageResource(
350
412
 
@@ -354,7 +416,7 @@
354
416
 
355
417
 
356
418
 
357
- //正解をrightAnswerにセット
419
+ //正解をrightAnswerにセット
358
420
 
359
421
  rightAnswer = quiz.get(1);
360
422
 
@@ -368,15 +430,15 @@
368
430
 
369
431
 
370
432
 
371
- //どのボタンが押されたか
433
+ //どのボタンが押されたか
372
434
 
373
435
  Button answerBtn = findViewById(view.getId());
374
436
 
375
437
 
376
438
 
377
- //ボタンのIdをテキストで取得・・・ここが分かりません
439
+ //ボタンのIdをテキストで取得・・・ここが分かりません
378
-
440
+
379
- String btnId = answerBtn.getText().toString(); //intString型に変換できない!というエラーが
441
+ String btnId = answerBtn.getText().toString(); //エラー: intは間接参照できませんというエラーが
380
442
 
381
443
 
382
444
 
@@ -386,19 +448,59 @@
386
448
 
387
449
 
388
450
 
389
-
390
-
391
451
  alertTitle = "せいかい!!";
392
452
 
393
453
  rightAnswerCount++;
394
454
 
395
455
 
396
456
 
397
-
457
+ } else {
458
+
398
-
459
+ alertTitle = "まちがっているよ";
460
+
399
-
461
+ } //if elseの波かっこ群終了
462
+
463
+
464
+
400
-
465
+ //ダイアログを作成
466
+
401
-
467
+ AlertDialog.Builder builder = new AlertDialog.Builder(this);
468
+
469
+ builder.setTitle(alertTitle);
470
+
471
+ builder.setMessage("こたえは" + rightAnswer + "だよ");
472
+
473
+ builder.setPositiveButton("つぎへいく", new DialogInterface.OnClickListener() {
474
+
475
+ @Override
476
+
477
+ public void onClick(DialogInterface dialog, int which) {
478
+
479
+ if (quizCount == QUIZ_COUNT) {
480
+
481
+ //結果画面へ移動
482
+
483
+ Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
484
+
485
+ intent.putExtra("RIGHT_ANSWER_COUNT", rightAnswerCount);
486
+
487
+ startActivity(intent);
488
+
489
+
490
+
491
+ } else {
492
+
493
+ quizCount++;
494
+
495
+ showNextQuiz(); //showNextQuiz実行する
496
+
497
+ } //elseとじ
498
+
499
+
500
+
501
+ } //160行付近のpublic void onClick(DialogInterface dialog, int which) とじ
502
+
503
+ }); //"つぎへいく"のsetPositeveButton とじ
402
504
 
403
505
  builder.setCancelable(false);
404
506
 
@@ -408,11 +510,17 @@
408
510
 
409
511
 
410
512
 
411
-
513
+ } // checkAnswer波かっことじ
412
-
514
+
515
+
516
+
413
- }
517
+ } //大きいMainとじ
414
-
415
- }
518
+
519
+
520
+
521
+
522
+
523
+
416
524
 
417
525
 
418
526
 

6

修正

2019/12/26 15:54

投稿

kubonos
kubonos

スコア6

test CHANGED
File without changes
test CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
 
22
22
 
23
- としているボタンの中のIDを、javaの中でString型として使う方法はありますでしょうか?
23
+ としているボタンの中のandroid:id="@+id/answerBtn1"を、javaの中でString型として使う方法はありますでしょうか?
24
24
 
25
25
 
26
26
 
@@ -28,7 +28,7 @@
28
28
 
29
29
  answerBtn1
30
30
 
31
- というテストとして、扱いたいのです。
31
+ というテストとして、扱いたいのです。
32
32
 
33
33
 
34
34
 
@@ -40,7 +40,7 @@
40
40
 
41
41
  android:id="@+id/answerBtn1
42
42
 
43
- のanswerBtn1というのを、正解判定の処理に使いたくて、
43
+ answerBtn1というのを、正解判定の処理に使いたくて、処理をどうしたら良いのか悩んでいます。
44
44
 
45
45
 
46
46
 
@@ -56,6 +56,12 @@
56
56
 
57
57
 
58
58
 
59
+
60
+
61
+ <やってみたこと>
62
+
63
+
64
+
59
65
  //どのボタンが押されたか
60
66
 
61
67
  Button answerBtn = findViewById(view.getId());
@@ -70,9 +76,15 @@
70
76
 
71
77
 
72
78
 
79
+
80
+
81
+
82
+
73
- 現在は、textカラーを00000000の透明色にして対応しているのですが、ボタンのtextは後から使う予定なので、なんとかIDなどで処理できないかなと思っています。
83
+ 現在は、android:text="A"のテキストカラーを00000000の透明色にして、クイズの正誤判定に対応しているのですが、ボタンのtextは後から使う予定なので、なんとかIDなどで処理できないかなと思っています。
84
+
85
+
86
+
74
-
87
+ テキストと違っていても、正解、という問題が混じっているため、正誤判定と、実際の答えはこれ!という表示が2通り必要なのです。
75
-
76
88
 
77
89
 
78
90
 
@@ -240,9 +252,9 @@
240
252
 
241
253
  //,←これ[,]が必要なんだ!!!
242
254
 
243
- {"c","c"},
255
+ {"a","b"},
244
-
256
+
245
- {"d","d"},
257
+ {"b","b"},
246
258
 
247
259
 
248
260
 
@@ -348,10 +360,6 @@
348
360
 
349
361
 
350
362
 
351
-
352
-
353
-
354
-
355
363
  }
356
364
 
357
365
 
@@ -414,7 +422,7 @@
414
422
 
415
423
 
416
424
 
417
- 具体的なサンプルコードをいただけませんでしょうか?
425
+ 1~2行で良いので、具体的なサンプルコードをいただけませんでしょうか?
418
426
 
419
427
 
420
428
 

5

修正

2019/12/26 15:41

投稿

kubonos
kubonos

スコア6

test CHANGED
File without changes
test CHANGED
@@ -56,6 +56,20 @@
56
56
 
57
57
 
58
58
 
59
+ //どのボタンが押されたか
60
+
61
+ Button answerBtn = findViewById(view.getId());
62
+
63
+
64
+
65
+ //IDをストリングで取得・・・ここがうまくいきません
66
+
67
+ String btnId = answerBtn.getText().toString();としてみたのですが
68
+
69
+ //int型はString型に変換できない!というエラーが出ています。
70
+
71
+
72
+
59
73
  現在は、textカラーを00000000の透明色にして対応しているのですが、ボタンのtextは後から使う予定なので、なんとかIDなどで処理できないかなと思っています。
60
74
 
61
75
 
@@ -352,15 +366,15 @@
352
366
 
353
367
 
354
368
 
355
- //ボタンのテキスト取得
369
+ //ボタンのIdをテキスト取得・・・ここが分かりません
356
-
370
+
357
- String btnText = answerBtn.getText().toString();
371
+ String btnId = answerBtn.getText().toString(); //int型はString型に変換できない!というエラーが
358
372
 
359
373
 
360
374
 
361
375
  String alertTitle;
362
376
 
363
- if (btnText.equals(rightAnswer)) { //ここをボタンID=rightAnswerに変えたい
377
+ if (btnId.equals(rightAnswer)) { //ここをボタンID=rightAnswerに変えたい
364
378
 
365
379
 
366
380
 

4

修正

2019/12/26 15:34

投稿

kubonos
kubonos

スコア6

test CHANGED
File without changes
test CHANGED
@@ -72,354 +72,336 @@
72
72
 
73
73
 
74
74
 
75
+ <?xml version="1.0" encoding="utf-8"?>
76
+
77
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
78
+
79
+ xmlns:app="http://schemas.android.com/apk/res-auto"
80
+
81
+ xmlns:tools="http://schemas.android.com/tools"
82
+
83
+ android:layout_width="match_parent"
84
+
85
+
86
+
87
+
88
+
89
+ android:layout_height="fill_parent"
90
+
91
+
92
+
93
+ tools:context=".MainActivity"
94
+
95
+ android:orientation="vertical"
96
+
97
+ android:gravity="center_horizontal"
98
+
99
+ android:background="@android:color/white">
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+ <ImageView
108
+
109
+ android:id="@+id/questionImage"
110
+
111
+ android:layout_width="300dp"
112
+
113
+ android:layout_height="200dp"
114
+
115
+ android:src="@drawable/a"/>
116
+
117
+
118
+
119
+
120
+
121
+ <LinearLayout
122
+
123
+ android:layout_width="fill_parent"
124
+
125
+
126
+
127
+ android:layout_height="fill_parent"
128
+
129
+
130
+
131
+ android:orientation="horizontal"
132
+
133
+ android:gravity="center_horizontal"
134
+
135
+ >
136
+
137
+
138
+
139
+ <Button
140
+
141
+ android:id="@+id/answerBtn1"
142
+
143
+ android:layout_width="60dp"
144
+
145
+ android:layout_height="60dp"
146
+
147
+ android:onClick="checkAnswer"
148
+
149
+ android:text="c" />
150
+
151
+
152
+
153
+
154
+
155
+ </LinearLayout>
156
+
157
+ </LinearLayout>
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+ package com.te.gazouonseijava;
166
+
167
+
168
+
169
+ import androidx.appcompat.app.AlertDialog;
170
+
171
+ import androidx.appcompat.app.AppCompatActivity;
172
+
173
+
174
+
175
+ import android.content.DialogInterface;
176
+
177
+ import android.content.Intent;
178
+
179
+ import android.os.Bundle;
180
+
181
+ import android.view.View;
182
+
183
+ import android.widget.Button;
184
+
185
+ import android.widget.ImageView;
186
+
187
+ import android.widget.TextView;
188
+
189
+
190
+
191
+ import java.util.ArrayList;
192
+
193
+ import java.util.Collections;
194
+
195
+ import java.util.Random;
196
+
197
+
198
+
199
+ public class MainActivity extends AppCompatActivity {
200
+
201
+
202
+
203
+ private TextView countLabel;
204
+
205
+
206
+
207
+ private ImageView questionImage;
208
+
209
+
210
+
211
+ //private TextView questionLabel;
212
+
213
+ private Button answerBtn1;
214
+
215
+
216
+
217
+
218
+
219
+ ArrayList<ArrayList<String>> quizArray = new ArrayList<>();
220
+
221
+
222
+
223
+ String quizData[][] = {
224
+
225
+ //{"画像名","正解"},
226
+
227
+ //,←これ[,]が必要なんだ!!!
228
+
229
+ {"c","c"},
230
+
231
+ {"d","d"},
232
+
233
+
234
+
235
+
236
+
237
+ };
238
+
239
+
240
+
241
+
242
+
243
+
244
+
245
+ @Override
246
+
247
+ protected void onCreate(Bundle savedInstanceState) {
248
+
249
+ super.onCreate(savedInstanceState);
250
+
251
+ setContentView(R.layout.activity_main);
252
+
253
+
254
+
255
+ soundPlayer = new SoundPlayer(this);
256
+
257
+ questionImage = findViewById(R.id.questionImage);
258
+
259
+ answerBtn1 = findViewById(R.id.answerBtn1);
260
+
261
+
262
+
263
+
264
+
265
+ //quizDataからクイズ出題用のquizArrayを作成する
266
+
267
+ for (int i = 0; i < quizData.length; i++) {
268
+
269
+
270
+
271
+ //新しいArrayListを用意
272
+
273
+ ArrayList<String> tmpArray = new ArrayList<>();
274
+
275
+ //クイズデータを追加
276
+
277
+ tmpArray.add(quizData[i][0]); //画像名
278
+
279
+
280
+
281
+
282
+
283
+ //tmpArrayをquizArrayに追加する
284
+
285
+ quizArray.add(tmpArray);
286
+
287
+ }
288
+
289
+
290
+
291
+ showNextQuiz();
292
+
293
+
294
+
295
+ }
296
+
297
+
298
+
299
+ public void showNextQuiz() {
300
+
301
+ //クイズカウントラベルを更新
302
+
303
+ countLabel.setText("Q" + quizCount);
304
+
305
+
306
+
307
+ //ランダムな数字を取得
308
+
309
+ Random random = new Random();
310
+
311
+ int randomNum = random.nextInt(quizArray.size());
312
+
313
+
314
+
315
+ //randomNumを使ってquizArrayからクイズを一つ取り出す
316
+
317
+ ArrayList<String> quiz = quizArray.get(randomNum);
318
+
319
+
320
+
321
+ //画像をセットする
322
+
323
+ questionImage.setImageResource(
324
+
325
+ getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
326
+
327
+ );
328
+
329
+
330
+
331
+ //正解をrightAnswerにセット
332
+
333
+ rightAnswer = quiz.get(1);
334
+
335
+
336
+
337
+
338
+
339
+
340
+
341
+ }
342
+
343
+
344
+
345
+ public void checkAnswer(View view){
346
+
347
+
348
+
349
+ //どのボタンが押されたか
350
+
351
+ Button answerBtn = findViewById(view.getId());
352
+
353
+
354
+
355
+ //ボタンのテキストを取得
356
+
357
+ String btnText = answerBtn.getText().toString();
358
+
359
+
360
+
361
+ String alertTitle;
362
+
363
+ if (btnText.equals(rightAnswer)) { //ここをボタンID=rightAnswerに変えたい
364
+
365
+
366
+
367
+
368
+
369
+ alertTitle = "せいかい!!";
370
+
371
+ rightAnswerCount++;
372
+
373
+
374
+
375
+
376
+
377
+
378
+
379
+
380
+
381
+ builder.setCancelable(false);
382
+
383
+ builder.show();
384
+
385
+
386
+
387
+
388
+
389
+
390
+
391
+ }
392
+
393
+ }
394
+
395
+
396
+
397
+
398
+
399
+ こんな感じに書いてみれば?みたいなサンプルコードをいただけると助かります・・。
400
+
401
+
402
+
75
403
  具体的なサンプルコードをいただけませんでしょうか?
76
404
 
77
405
 
78
406
 
79
- サンプルコードをいただけますと大変助かります。
80
-
81
-
82
-
83
- 大変申し訳ございませんが、サンプルコードをよろしくお願いいたします。
407
+ 大変申し訳ございませんが、javaの理解力が低いため、サンプルコードをよろしくお願いいたします。
84
-
85
-
86
-
87
-
88
-
89
- <?xml version="1.0" encoding="utf-8"?>
90
-
91
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
92
-
93
- xmlns:app="http://schemas.android.com/apk/res-auto"
94
-
95
- xmlns:tools="http://schemas.android.com/tools"
96
-
97
- android:layout_width="match_parent"
98
-
99
-
100
-
101
-
102
-
103
- android:layout_height="fill_parent"
104
-
105
-
106
-
107
- tools:context=".MainActivity"
108
-
109
- android:orientation="vertical"
110
-
111
- android:gravity="center_horizontal"
112
-
113
- android:background="@android:color/white">
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
- <ImageView
122
-
123
- android:id="@+id/questionImage"
124
-
125
- android:layout_width="300dp"
126
-
127
- android:layout_height="200dp"
128
-
129
- android:src="@drawable/a"/>
130
-
131
-
132
-
133
-
134
-
135
- <LinearLayout
136
-
137
- android:layout_width="fill_parent"
138
-
139
-
140
-
141
- android:layout_height="fill_parent"
142
-
143
-
144
-
145
- android:orientation="horizontal"
146
-
147
- android:gravity="center_horizontal"
148
-
149
- >
150
-
151
-
152
-
153
- <Button
154
-
155
- android:id="@+id/answerBtn1"
156
-
157
- android:layout_width="60dp"
158
-
159
- android:layout_height="60dp"
160
-
161
- android:onClick="checkAnswer"
162
-
163
- android:text="c" />
164
-
165
-
166
-
167
-
168
-
169
- </LinearLayout>
170
-
171
- </LinearLayout>
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
- package com.te.gazouonseijava;
180
-
181
-
182
-
183
- import androidx.appcompat.app.AlertDialog;
184
-
185
- import androidx.appcompat.app.AppCompatActivity;
186
-
187
-
188
-
189
- import android.content.DialogInterface;
190
-
191
- import android.content.Intent;
192
-
193
- import android.os.Bundle;
194
-
195
- import android.view.View;
196
-
197
- import android.widget.Button;
198
-
199
- import android.widget.ImageView;
200
-
201
- import android.widget.TextView;
202
-
203
-
204
-
205
- import java.util.ArrayList;
206
-
207
- import java.util.Collections;
208
-
209
- import java.util.Random;
210
-
211
-
212
-
213
- public class MainActivity extends AppCompatActivity {
214
-
215
-
216
-
217
- private TextView countLabel;
218
-
219
-
220
-
221
- private ImageView questionImage;
222
-
223
-
224
-
225
- //private TextView questionLabel;
226
-
227
- private Button answerBtn1;
228
-
229
-
230
-
231
-
232
-
233
- ArrayList<ArrayList<String>> quizArray = new ArrayList<>();
234
-
235
-
236
-
237
- String quizData[][] = {
238
-
239
- //{"画像名","正解"},
240
-
241
- //,←これ[,]が必要なんだ!!!
242
-
243
- {"c","c"},
244
-
245
- {"d","d"},
246
-
247
-
248
-
249
-
250
-
251
- };
252
-
253
-
254
-
255
-
256
-
257
-
258
-
259
- @Override
260
-
261
- protected void onCreate(Bundle savedInstanceState) {
262
-
263
- super.onCreate(savedInstanceState);
264
-
265
- setContentView(R.layout.activity_main);
266
-
267
-
268
-
269
- soundPlayer = new SoundPlayer(this);
270
-
271
- questionImage = findViewById(R.id.questionImage);
272
-
273
- answerBtn1 = findViewById(R.id.answerBtn1);
274
-
275
-
276
-
277
-
278
-
279
- //quizDataからクイズ出題用のquizArrayを作成する
280
-
281
- for (int i = 0; i < quizData.length; i++) {
282
-
283
-
284
-
285
- //新しいArrayListを用意
286
-
287
- ArrayList<String> tmpArray = new ArrayList<>();
288
-
289
- //クイズデータを追加
290
-
291
- tmpArray.add(quizData[i][0]); //画像名
292
-
293
-
294
-
295
-
296
-
297
- //tmpArrayをquizArrayに追加する
298
-
299
- quizArray.add(tmpArray);
300
-
301
- }
302
-
303
-
304
-
305
- showNextQuiz();
306
-
307
-
308
-
309
- }
310
-
311
-
312
-
313
- public void showNextQuiz() {
314
-
315
- //クイズカウントラベルを更新
316
-
317
- countLabel.setText("Q" + quizCount);
318
-
319
-
320
-
321
- //ランダムな数字を取得
322
-
323
- Random random = new Random();
324
-
325
- int randomNum = random.nextInt(quizArray.size());
326
-
327
-
328
-
329
- //randomNumを使ってquizArrayからクイズを一つ取り出す
330
-
331
- ArrayList<String> quiz = quizArray.get(randomNum);
332
-
333
-
334
-
335
- //画像をセットする
336
-
337
- questionImage.setImageResource(
338
-
339
- getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
340
-
341
- );
342
-
343
-
344
-
345
- //正解をrightAnswerにセット
346
-
347
- rightAnswer = quiz.get(1);
348
-
349
-
350
-
351
-
352
-
353
-
354
-
355
- }
356
-
357
-
358
-
359
- public void checkAnswer(View view){
360
-
361
-
362
-
363
- //どのボタンが押されたか
364
-
365
- Button answerBtn = findViewById(view.getId());
366
-
367
-
368
-
369
- //ボタンのテキストを取得
370
-
371
- String btnText = answerBtn.getText().toString();
372
-
373
-
374
-
375
- String alertTitle;
376
-
377
- if (btnText.equals(rightAnswer)) { //ここをボタンID=rightAnswerに変えたい
378
-
379
-
380
-
381
-
382
-
383
- alertTitle = "せいかい!!";
384
-
385
- rightAnswerCount++;
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
- builder.setCancelable(false);
396
-
397
- builder.show();
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
- }
406
-
407
- }
408
-
409
-
410
-
411
-
412
-
413
- こんな感じに書いてみれば?みたいなサンプルコードをいただけると助かります・・。
414
-
415
-
416
-
417
- 具体的なサンプルコードをいただけませんでしょうか?
418
-
419
-
420
-
421
- サンプルコードをいただけますと大変助かります。
422
-
423
-
424
-
425
- 大変申し訳ございませんが、サンプルコードをよろしくお願いいたします。

3

修正

2019/12/26 11:42

投稿

kubonos
kubonos

スコア6

test CHANGED
File without changes
test CHANGED
@@ -411,3 +411,15 @@
411
411
 
412
412
 
413
413
  こんな感じに書いてみれば?みたいなサンプルコードをいただけると助かります・・。
414
+
415
+
416
+
417
+ 具体的なサンプルコードをいただけませんでしょうか?
418
+
419
+
420
+
421
+ サンプルコードをいただけますと大変助かります。
422
+
423
+
424
+
425
+ 大変申し訳ございませんが、サンプルコードをよろしくお願いいたします。

2

誤字

2019/12/26 08:29

投稿

kubonos
kubonos

スコア6

test CHANGED
File without changes
test CHANGED
@@ -56,6 +56,18 @@
56
56
 
57
57
 
58
58
 
59
+ 現在は、textカラーを00000000の透明色にして対応しているのですが、ボタンのtextは後から使う予定なので、なんとかIDなどで処理できないかなと思っています。
60
+
61
+
62
+
63
+
64
+
65
+ android:text
66
+
67
+ でないもので、かつ、String型で参照できるものでも良いのですが・・
68
+
69
+
70
+
59
71
 
60
72
 
61
73
 
@@ -240,11 +252,7 @@
240
252
 
241
253
 
242
254
 
243
- private SoundPlayer soundPlayer;
255
+
244
-
245
-
246
-
247
-
248
256
 
249
257
 
250
258
 
@@ -360,13 +368,17 @@
360
368
 
361
369
  //ボタンのテキストを取得
362
370
 
363
- String btnText = answerBtn.getText().toString(); //ここをIDから取得に変えたい
371
+ String btnText = answerBtn.getText().toString();
364
372
 
365
373
 
366
374
 
367
375
  String alertTitle;
368
376
 
369
- if (btnText.equals(rightAnswer)) {
377
+ if (btnText.equals(rightAnswer)) { //ここをボタンID=rightAnswerに変えたい
378
+
379
+
380
+
381
+
370
382
 
371
383
  alertTitle = "せいかい!!";
372
384
 
@@ -393,3 +405,9 @@
393
405
  }
394
406
 
395
407
  }
408
+
409
+
410
+
411
+
412
+
413
+ こんな感じに書いてみれば?みたいなサンプルコードをいただけると助かります・・。

1

修正

2019/12/26 08:28

投稿

kubonos
kubonos

スコア6

test CHANGED
File without changes
test CHANGED
@@ -69,3 +69,327 @@
69
69
 
70
70
 
71
71
  大変申し訳ございませんが、サンプルコードをよろしくお願いいたします。
72
+
73
+
74
+
75
+
76
+
77
+ <?xml version="1.0" encoding="utf-8"?>
78
+
79
+ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
80
+
81
+ xmlns:app="http://schemas.android.com/apk/res-auto"
82
+
83
+ xmlns:tools="http://schemas.android.com/tools"
84
+
85
+ android:layout_width="match_parent"
86
+
87
+
88
+
89
+
90
+
91
+ android:layout_height="fill_parent"
92
+
93
+
94
+
95
+ tools:context=".MainActivity"
96
+
97
+ android:orientation="vertical"
98
+
99
+ android:gravity="center_horizontal"
100
+
101
+ android:background="@android:color/white">
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+ <ImageView
110
+
111
+ android:id="@+id/questionImage"
112
+
113
+ android:layout_width="300dp"
114
+
115
+ android:layout_height="200dp"
116
+
117
+ android:src="@drawable/a"/>
118
+
119
+
120
+
121
+
122
+
123
+ <LinearLayout
124
+
125
+ android:layout_width="fill_parent"
126
+
127
+
128
+
129
+ android:layout_height="fill_parent"
130
+
131
+
132
+
133
+ android:orientation="horizontal"
134
+
135
+ android:gravity="center_horizontal"
136
+
137
+ >
138
+
139
+
140
+
141
+ <Button
142
+
143
+ android:id="@+id/answerBtn1"
144
+
145
+ android:layout_width="60dp"
146
+
147
+ android:layout_height="60dp"
148
+
149
+ android:onClick="checkAnswer"
150
+
151
+ android:text="c" />
152
+
153
+
154
+
155
+
156
+
157
+ </LinearLayout>
158
+
159
+ </LinearLayout>
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+ package com.te.gazouonseijava;
168
+
169
+
170
+
171
+ import androidx.appcompat.app.AlertDialog;
172
+
173
+ import androidx.appcompat.app.AppCompatActivity;
174
+
175
+
176
+
177
+ import android.content.DialogInterface;
178
+
179
+ import android.content.Intent;
180
+
181
+ import android.os.Bundle;
182
+
183
+ import android.view.View;
184
+
185
+ import android.widget.Button;
186
+
187
+ import android.widget.ImageView;
188
+
189
+ import android.widget.TextView;
190
+
191
+
192
+
193
+ import java.util.ArrayList;
194
+
195
+ import java.util.Collections;
196
+
197
+ import java.util.Random;
198
+
199
+
200
+
201
+ public class MainActivity extends AppCompatActivity {
202
+
203
+
204
+
205
+ private TextView countLabel;
206
+
207
+
208
+
209
+ private ImageView questionImage;
210
+
211
+
212
+
213
+ //private TextView questionLabel;
214
+
215
+ private Button answerBtn1;
216
+
217
+
218
+
219
+
220
+
221
+ ArrayList<ArrayList<String>> quizArray = new ArrayList<>();
222
+
223
+
224
+
225
+ String quizData[][] = {
226
+
227
+ //{"画像名","正解"},
228
+
229
+ //,←これ[,]が必要なんだ!!!
230
+
231
+ {"c","c"},
232
+
233
+ {"d","d"},
234
+
235
+
236
+
237
+
238
+
239
+ };
240
+
241
+
242
+
243
+ private SoundPlayer soundPlayer;
244
+
245
+
246
+
247
+
248
+
249
+
250
+
251
+ @Override
252
+
253
+ protected void onCreate(Bundle savedInstanceState) {
254
+
255
+ super.onCreate(savedInstanceState);
256
+
257
+ setContentView(R.layout.activity_main);
258
+
259
+
260
+
261
+ soundPlayer = new SoundPlayer(this);
262
+
263
+ questionImage = findViewById(R.id.questionImage);
264
+
265
+ answerBtn1 = findViewById(R.id.answerBtn1);
266
+
267
+
268
+
269
+
270
+
271
+ //quizDataからクイズ出題用のquizArrayを作成する
272
+
273
+ for (int i = 0; i < quizData.length; i++) {
274
+
275
+
276
+
277
+ //新しいArrayListを用意
278
+
279
+ ArrayList<String> tmpArray = new ArrayList<>();
280
+
281
+ //クイズデータを追加
282
+
283
+ tmpArray.add(quizData[i][0]); //画像名
284
+
285
+
286
+
287
+
288
+
289
+ //tmpArrayをquizArrayに追加する
290
+
291
+ quizArray.add(tmpArray);
292
+
293
+ }
294
+
295
+
296
+
297
+ showNextQuiz();
298
+
299
+
300
+
301
+ }
302
+
303
+
304
+
305
+ public void showNextQuiz() {
306
+
307
+ //クイズカウントラベルを更新
308
+
309
+ countLabel.setText("Q" + quizCount);
310
+
311
+
312
+
313
+ //ランダムな数字を取得
314
+
315
+ Random random = new Random();
316
+
317
+ int randomNum = random.nextInt(quizArray.size());
318
+
319
+
320
+
321
+ //randomNumを使ってquizArrayからクイズを一つ取り出す
322
+
323
+ ArrayList<String> quiz = quizArray.get(randomNum);
324
+
325
+
326
+
327
+ //画像をセットする
328
+
329
+ questionImage.setImageResource(
330
+
331
+ getResources().getIdentifier(quiz.get(0), "drawable", getPackageName())
332
+
333
+ );
334
+
335
+
336
+
337
+ //正解をrightAnswerにセット
338
+
339
+ rightAnswer = quiz.get(1);
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+ }
348
+
349
+
350
+
351
+ public void checkAnswer(View view){
352
+
353
+
354
+
355
+ //どのボタンが押されたか
356
+
357
+ Button answerBtn = findViewById(view.getId());
358
+
359
+
360
+
361
+ //ボタンのテキストを取得
362
+
363
+ String btnText = answerBtn.getText().toString(); //ここをIDから取得に変えたい
364
+
365
+
366
+
367
+ String alertTitle;
368
+
369
+ if (btnText.equals(rightAnswer)) {
370
+
371
+ alertTitle = "せいかい!!";
372
+
373
+ rightAnswerCount++;
374
+
375
+
376
+
377
+
378
+
379
+
380
+
381
+
382
+
383
+ builder.setCancelable(false);
384
+
385
+ builder.show();
386
+
387
+
388
+
389
+
390
+
391
+
392
+
393
+ }
394
+
395
+ }