Advertisement
sphinx2001

Quiz - main activity

Mar 9th, 2024
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.18 KB | None | 0 0
  1. package oek.colledge.quiz3pr
  2.  
  3. import android.content.Intent
  4. import android.os.Bundle
  5. import android.widget.Button
  6. import android.widget.TextView
  7. import android.widget.Toast
  8. import androidx.appcompat.app.AppCompatActivity
  9. import org.w3c.dom.Text
  10.  
  11. class MainActivity : AppCompatActivity() {
  12.     var question = listOf(
  13.         "Сколько будет 2 + 2?\n\nA) 4\n\nB) 5\n\nC) 6",
  14.         "Какие основные языки программирования для Android?\n\nA) Kotlin и Java\n\nB) Python и Java\n\nC) C++ и C#",
  15.         "Как называется окно в Android приложении?\n\nA) Activity\n\nB) Dialog\n\nC) Fragment"
  16.     )
  17.     var rightAnswer = listOf(1, 1, 1)
  18.     var questionIndex = -1
  19.     var answers: MutableList<Int> = mutableListOf()
  20.  
  21.     override fun onCreate(savedInstanceState: Bundle?) {
  22.         super.onCreate(savedInstanceState)
  23.         setContentView(R.layout.activity_main)
  24.  
  25.         UpdateQuestion()
  26.  
  27.         findViewById<Button>(R.id.button1).setOnClickListener {
  28.             GetAnswer(1)
  29.         }
  30.         findViewById<Button>(R.id.button2).setOnClickListener {
  31.             GetAnswer(2)
  32.         }
  33.         findViewById<Button>(R.id.button3).setOnClickListener {
  34.             GetAnswer(3)
  35.         }
  36.  
  37.     }
  38.  
  39.     fun GetAnswer(answer: Int) {
  40.         if (answer == rightAnswer[questionIndex]) {
  41.             Toast.makeText(this, "Верно!", Toast.LENGTH_SHORT).show()
  42.             answers.add(1)
  43.             UpdateQuestion()
  44.         } else {
  45.             answers.add(-1)
  46.             Toast.makeText(this, "Неверно!", Toast.LENGTH_SHORT).show()
  47.         }
  48.     }
  49.  
  50.     fun UpdateQuestion() {
  51.         questionIndex++
  52.         if (questionIndex < question.size) {
  53.             findViewById<TextView>(R.id.tvQuest).text = question[questionIndex]
  54.             return
  55.         }
  56.         Toast.makeText(this, "Тест завершен!", Toast.LENGTH_SHORT).show()
  57.         var intentResult = Intent(this, ResultTestActivity::class.java)
  58.         var score = answers.sum()
  59.         intentResult.putExtra("score", score)
  60.         intentResult.putExtra("countQuestion", question.size)
  61.         startActivity(intentResult)
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement