1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| package com.test.Quiz
import android.content.DialogInterface
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_quiz2.*
import java.net.IDN
class QuizActivity2 : AppCompatActivity() {
var quizs = ArrayList<Quiz>()
var numberOfGoodAnswers: Int = 0
var currentQuizIndex: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz2)
quizs.add(Quiz("Quelle est Capital Alger?", "Alger", "Fez","Marakech",1))
quizs.add(Quiz("Quelle est Capital Italy?", "Venise", "Rome","turin",2))
quizs.add(Quiz("Quelle est Capital l'Ongola?", "Kameroune", "Valana","Luanda",3))
quizs.add(Quiz("Quelle est Capital Angletere?", "beach", "Suncity","Londre",3))
showQestion(quizs.get(currentQuizIndex))
}
fun showQestion(quiz: Quiz) {
txtQuestion.setText(quiz.question)
answer1.setText(quiz.answer1)
answer2.setText(quiz.answer2)
answer3.setText(quiz.answer3)
}
fun handleAnswer(answerID: Int) {
val quiz = quizs.get(currentQuizIndex)
if (quiz.isCorrect(answerID)) {
numberOfGoodAnswers++
Toast.makeText(this, "+1", Toast.LENGTH_SHORT).show()
}
//j'ai déplacé currentQuizIndex++ ici Pour pouvoir aller à la question suivante si bonne réponse normalement il est placé après expression Else
currentQuizIndex++
else {
Toast.makeText(this, "+0", Toast.LENGTH_SHORT).show()
}
// maintenant comment faire à partir d'ici pour retourner à la question précédente étant donné que la réponse est fausse ?
if (currentQuizIndex >= quizs.size) { // Partie terminé
var alert = AlertDialog.Builder(this)
alert.setTitle("Partie terminé!")
alert.setMessage("Tu as eu : " + numberOfGoodAnswers + " bonne(s) réponse(s)")
alert.setPositiveButton("OK") { dialogInterface: DialogInterface?, i: Int ->
finish()
}
alert.show()
} else {// on continue la partie
showQestion(quizs.get(currentQuizIndex))
}
}
fun onClickAnwerOne(view: View) {
handleAnswer(1)
}
fun onClickAnwerTwo(view: View) {
handleAnswer(2)pl
}
fun onClickAnwerThree(view: View) {
handleAnswer(3)
}
}
}
fun onClickAnwerThree(view: View) {
handleAnswer(3)
}
}
}
} |
Partager