Изменить цвет фона ответов в приложении викторины ⇐ 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
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
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Создание викторины на Java с использованием массивов. Нужны указатели с оценкой ответов
Anonymous » » в форуме JAVA - 0 Ответы
- 20 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Удаленная форма Rails UJS оценивает JS только для ответов 2XX, но не для ответов 4XX
Anonymous » » в форуме Jquery - 0 Ответы
- 59 Просмотры
-
Последнее сообщение Anonymous
-