【Android studio】Java タイム結果の表示について
Q&A
Closed
解決したいこと
Android studio Javaで、クイズアプリを作っています。
サンプルコードを見ながら作っている初心者です。
クイズ解答にかかった時間を、結果画面(次ページの別画面)に表示したいです。
クイズ実行中にストップウォッチを動かすことはできたのですが、結果画面(ResultActivity)へタイムの結果を表示する方法が分かりません。
教えて頂けると幸いです。
該当するソースコード
public class SabActivity extends AppCompatActivity {
TextView clearTime;
Handler handler;
Runnable runnable;
long startTime, elapsedTime;
private TextView countLabel;
private TextView questionLabel;
private Button answerBtn1;
private Button answerBtn2;
private Button answerBtn3;
private Button answerBtn4;
private String rightAnswer;
private int rightAnswerCount;
private int quizCount = 1;
static final private int QUIZ_COUNT = 10;
ArrayList<ArrayList<String>> quizArray = new ArrayList<>();
String[][] quizData = {
// {"問題", "正解", "選択肢1", "選択肢2", "選択肢3"}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sab);
//タイムスタート
onTimer();
clearTime = findViewById(R.id.text_clearTime);
countLabel = findViewById(R.id.countLabel);
questionLabel = findViewById(R.id.questionLabel);
answerBtn1 = findViewById(R.id.answerBtn1);
answerBtn2 = findViewById(R.id.answerBtn2);
answerBtn3 = findViewById(R.id.answerBtn3);
answerBtn4 = findViewById(R.id.answerBtn4);
for (String[] quizDatum : quizData) {
ArrayList<String> tmpArray = new ArrayList<>();
tmpArray.add(quizDatum[0]); // 問題
tmpArray.add(quizDatum[1]); // 正解
tmpArray.add(quizDatum[2]); // 選択肢1
tmpArray.add(quizDatum[3]); // 選択肢2
tmpArray.add(quizDatum[4]); // 選択肢3
quizArray.add(tmpArray);
}
showNextQuiz();
}
public void showNextQuiz() {
countLabel.setText(getString(R.string.count_label, quizCount));
Random random = new Random();
int randomNum = random.nextInt(quizArray.size());
ArrayList<String> quiz = quizArray.get(randomNum);
questionLabel.setText(quiz.get(0));
rightAnswer = quiz.get(1);
quiz.remove(0);
Collections.shuffle(quiz);
answerBtn1.setText(quiz.get(0));
answerBtn2.setText(quiz.get(1));
answerBtn3.setText(quiz.get(2));
answerBtn4.setText(quiz.get(3));
quizArray.remove(randomNum);
}
//ストップウォッチ
public void onTimer() {
handler = new Handler();
startTime = System.currentTimeMillis();
runnable = new Runnable() {
@Override
public void run() {
elapsedTime = System.currentTimeMillis() - startTime;
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss.SSS", Locale.US);
clearTime.setText(sdf.format(elapsedTime));
handler.removeCallbacks(runnable);
handler.postDelayed(runnable, 10);
}
};
handler.postDelayed(runnable,10);}
public void stopTimer() {
handler.removeCallbacks(runnable);
}
public void checkAnswer(View view){
ImageView imageView = new ImageView(this);
Button answerBtn = findViewById(view.getId());
String btnText = answerBtn.getText().toString();
if (btnText.equals(rightAnswer)) {
soundPool.play(mp3a, 1f, 1f, 0, 0, 1f);
imageView.setImageResource(R.drawable.usagi_maru);
rightAnswerCount++;
} else {
soundPool.play(mp3b, 1f, 1f, 0, 0, 1f);
imageView.setImageResource(R.drawable.usagi_batsu);
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(imageView);
builder.setMessage("こたえ : " + rightAnswer);
builder.setPositiveButton("つぎへ➡", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i){
if (quizCount == QUIZ_COUNT) {
Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", rightAnswerCount);
startActivity(intent);
//タイムストップ
stopTimer();
} else {
quizCount++;
showNextQuiz();
}
}
});
builder.setCancelable(false);
builder.show();
}