Изменить цвет фона ответов в приложении викториныAndroid

Форум для тех, кто программирует под Android
Ответить Пред. темаСлед. тема
Гость
 Изменить цвет фона ответов в приложении викторины

Сообщение Гость »


sorry for my ignorance. I implemented this quiz activity so that once the user has decided on the answer, he presses and the btn backround becomes light blue and then he has to press next to go to the next question. However, I would like that once the answer is pressed the application colors its green if it's correct. if not, color it red and color the right answer green. then he must press next to move to the next question. here is my QuizActivity.kt code

companion object { var questionModelList : List = listOf() var time : String = "" } lateinit var binding: ActivityQuizBinding var currentQuestionIndex = 0; var selectedAnswer = "" var score = 0; override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding= ActivityQuizBinding.inflate(layoutInflater) setContentView(binding.root) binding.apply { btn0.setOnClickListener(this@QuizActivity) btn1.setOnClickListener(this@QuizActivity) btn2.setOnClickListener(this@QuizActivity) nextBtn.setOnClickListener(this@QuizActivity) } loadQuestion() startTimer() } private fun startTimer(){ val totalTimeInMillis = time.toInt() * 60 * 1000; object : CountDownTimer(totalTimeInMillis.toLong(),1000L){ override fun onTick(millisUntilFinished: Long) { val seconds = millisUntilFinished / 1000 val minutes = seconds / 60 val remainingSeconds = seconds % 60 binding.timerIndicatorTextview.text = String.format("%02d:%02d", minutes,remainingSeconds) } override fun onFinish() { //finish the quiz } }.start() } private fun loadQuestion(){ selectedAnswer = "" if(currentQuestionIndex == questionModelList.size){ finishQuiz() return } binding.apply { questionIndicatorTextview.text = "Question ${currentQuestionIndex+1}/ ${questionModelList.size} " questionProgressIndicator.progress = (currentQuestionIndex.toFloat() / questionModelList.size.toFloat() * 100).toInt() questionTextview.text = questionModelList[currentQuestionIndex].question btn0.text = questionModelList[currentQuestionIndex].options[0] btn1.text = questionModelList[currentQuestionIndex].options[1] btn2.text = questionModelList[currentQuestionIndex].options[2] } } override fun onClick(view: View?) { binding.apply { btn0.setBackgroundColor(getColor(R.color.gray)) btn1.setBackgroundColor(getColor(R.color.gray)) btn2.setBackgroundColor(getColor(R.color.gray)) } val clickBtn = view as Button if(clickBtn.id ==R.id.next_btn){ //next button is clicked if(selectedAnswer == questionModelList[currentQuestionIndex].correct){ score++; Log.i("score of quiz",score.toString()) } currentQuestionIndex++ loadQuestion() }else{ selectedAnswer = clickBtn.text.toString() clickBtn.setBackgroundColor(getColor(R.color.lightblue)) } } private fun finishQuiz(){ val totalQuestions = questionModelList.size val percentage = (score.toFloat() / totalQuestions.toFloat() * 100).toInt() val dialogBinding = ScoreDialogBinding.inflate(layoutInflater) dialogBinding dialogBinding.apply { scoreProgressIndicator.progress = percentage scoreProgressText.text = "$percentage %" if(percentage>60){ scoreTitle.text = "Congrats! You have passed" scoreTitle.setTextColor(Color.BLUE) } else{ scoreTitle.text = "Oops! You have failed" scoreTitle.setTextColor(Color.RED) } scoreSubtitle.text = "$score out of $totalQuestions are correct" finishBtn.setOnClickListener({ finish() }) } AlertDialog.Builder(this) .setView(dialogBinding.root) .setCancelable(false) .show() } i tried to use chatgpt but i'm not good


Источник: https://stackoverflow.com/questions/780 ... a-quiz-app
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Android»