Contenidos
activity_main.xml
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 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textViewQuestion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¿Pregunta?" android:textSize="18sp" android:layout_marginTop="24dp" android:layout_centerHorizontal="true" /> <RadioGroup android:id="@+id/radioGroupOptions" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/textViewQuestion" android:layout_marginTop="16dp" android:layout_centerHorizontal="true"> <RadioButton android:id="@+id/radioButtonOption1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Opción 1" /> <RadioButton android:id="@+id/radioButtonOption2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Opción 2" /> <RadioButton android:id="@+id/radioButtonOption3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Opción 3" /> <RadioButton android:id="@+id/radioButtonOption4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Opción 4" /> </RadioGroup> <Button android:id="@+id/buttonSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Siguiente" android:layout_below="@id/radioGroupOptions" android:layout_marginTop="24dp" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/textViewResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Resultado" android:textSize="18sp" android:layout_below="@id/buttonSubmit" android:layout_marginTop="24dp" android:layout_centerHorizontal="true" /> </RelativeLayout> |
MainActivity.kt
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 98 99 100 101 102 103 104 105 106 |
package com.example.myapplication import android.os.Bundle import android.view.View import android.widget.Button import android.widget.RadioButton import android.widget.RadioGroup import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.core.view.isInvisible import androidx.core.view.isVisible class MainActivity : AppCompatActivity() { private lateinit var textViewQuestion: TextView private lateinit var radioGroupOptions: RadioGroup private lateinit var buttonSubmit: Button private lateinit var textViewResult: TextView private val questions = listOf( "¿Cuál es la capital de Francia?", "¿Cuál es la capital de Alemania?", "¿Cuál es la capital de Italia?" // Agrega más preguntas sobre capitales de Europa aquí ) private val options = listOf( listOf("París", "Madrid", "Londres", "Roma"), listOf("París", "Berlín", "Londres", "Roma"), listOf("París", "Berlín", "Londres", "Roma") // Agrega las opciones para cada pregunta aquí ) private val correctAnswers = listOf( "París", "Berlín", "Roma" // Agrega las respuestas correctas para cada pregunta aquí ) private var currentQuestionIndex = 0 private var score = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) textViewQuestion = findViewById(R.id.textViewQuestion) radioGroupOptions = findViewById(R.id.radioGroupOptions) buttonSubmit = findViewById(R.id.buttonSubmit) textViewResult = findViewById(R.id.textViewResult) displayQuestion() buttonSubmit.setOnClickListener { val selectedOptionId = radioGroupOptions.checkedRadioButtonId if (selectedOptionId != -1) { val selectedOption = findViewById<RadioButton>(selectedOptionId) val selectedAnswer = selectedOption.text.toString() if (selectedAnswer == correctAnswers[currentQuestionIndex]) { score++ } currentQuestionIndex++ if (currentQuestionIndex < questions.size) { displayQuestion() } else { displayScore() } } } } private fun displayQuestion() { if (currentQuestionIndex < questions.size) { textViewQuestion.text = questions[currentQuestionIndex] radioGroupOptions.clearCheck() val currentOptions = options[currentQuestionIndex] for (i in 0 until radioGroupOptions.childCount) { val radioButton = radioGroupOptions.getChildAt(i) as RadioButton if (i < currentOptions.size) { radioButton.text = currentOptions[i] radioButton.visibility = View.VISIBLE radioButton.isEnabled = true } else { radioButton.visibility = View.GONE radioButton.isEnabled = false } } buttonSubmit.text = "Siguiente" textViewResult.text = "Puntuación: $score" } else { textViewQuestion.text = "Examen completado" textViewResult.text = "Puntuación final: $score / ${questions.size}" radioGroupOptions.visibility = View.GONE buttonSubmit.isEnabled = false } } private fun displayScore() { textViewQuestion.text = "Examen completado" textViewResult.text = "Puntuación final: $score / ${questions.size}" buttonSubmit.isEnabled = false } } |