Kotlin Toast for a multi quiz

问题描述 投票:0回答:1

我是Kotlin的新手。我目前正在尝试在android studio中建立一个多问题测验,我想知道如何在单击“提交”后显示祝酒词,说明答案是正确还是不正确。我已经在processSubmitButtonClick()中开始了敬酒,但是我对如何让它知道是对还是错感到困惑。String.xml

<resources>
    <string name="app_name">AnswerButton</string>
    <string name="australia_question">What is the capital of Australia?</string>
    <string name="australia_answer_brisbane">Brisbane</string>
    <string name="australia_answer_canberra">Canberra</string>
    <string name="australia_answer_perth">Perth</string>
    <string name="australia_answer_sidney">Sidney</string>
    <string name="hint_button_text">Hint</string>
    <string name="submit_button_text">Submit</string>
    <string name="correct_toast">Correct!</string>
    <string name="incorrect_toast">Incorrect!</string>
</resources>

Answer.kt

package quiz

import androidx.annotation.StringRes


data class Answer (@StringRes val textResId: Int,
                   val isCorrect: Boolean,
                   var isEnabled: Boolean = true,
                   var isSelected: Boolean = false)

MainActivity.kt

package quiz

import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {

    private val defaultButtonColor = "#00a2ff"
    private val selectedButtonColor = "#cb297b"

    // view fields
    private lateinit var questionTextView: TextView
    private lateinit var answerButtonList: List<Button>
    private lateinit var hintButton: Button
    private lateinit var submitButton: Button

    //model fields

   private val answerList = listOf(
        Answer(R.string.australia_answer_brisbane, false),
        Answer(R.string.australia_answer_canberra, true),
        Answer(R.string.australia_answer_perth, false),
        Answer(R.string.australia_answer_sidney, false)
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // initialize views
        questionTextView = findViewById(R.id.question_text_view)
        answerButtonList = listOf(
            findViewById(R.id.answer_0_button),
            findViewById(R.id.answer_1_button),
            findViewById(R.id.answer_2_button),
            findViewById(R.id.answer_3_button)
        )
        hintButton = findViewById(R.id.hint_button)
        submitButton = findViewById(R.id.submit_button)

        // set question text
        questionTextView.setText(R.string.australia_question)

        // set text in answer button views
        for (index in 0..3) {
            answerButtonList[index].setText(answerList[index].textResId)
        }

        // set text for hint and submit buttons
        hintButton.setText(R.string.hint_button_text)
        submitButton.setText(R.string.submit_button_text)

        // attach listeners to answer buttons
        for((answer, button) in answerList.zip(answerButtonList)){
            button.setOnClickListener {
                processAnswerButtonClick(answer)
            }
        }

        // attach listeners to hint button and submit button
        hintButton.setOnClickListener {
            processHintButtonClick()
        }
        submitButton.setOnClickListener {
            processSubmitButtonClick()    
        }

        refreshView()
    }

    private fun processAnswerButtonClick(answer: Answer) {
        val origIsSelected = answer.isSelected

        // deselect all answers
        for (a in answerList.minus(answer)){
            a.isSelected = false
        }

        // toggle clicked button
        answer.isSelected = !origIsSelected

        // refresh the view
        refreshView()
    }

    private fun processHintButtonClick() {
        // hint the first two incorrect answers (even if one is selected)
        answerList
            .filterNot { e -> e.isCorrect }
            .take(2)
            .forEach { a ->
                a.isEnabled = false
                a.isSelected = false 
           }  

          refreshView()
    }

    private fun checkAnswer(userAnswer: Boolean){
       val correctAnswer = answerList[currentIndex].?

        val message = if (userAnswer == correctAnswer){

            R.string.correct_toast
        } else {
            R.string.incorrect_toast
        }
        Toast.makeText(this, message, Toast.LENGTH_SHORT) .show()

    }
private fun processSubmitButtonClick( ) {
    answerList
        .forEach { a ->
            a.isEnabled = true
            a.isSelected = false
        }

    for (answer in answerList) {
        if ((answer.isSelected) && (answer.isCorrect)) {
            checkAnswer(true)
        }
        else {
            checkAnswer(false)
        }

     }
        refreshView()
}

私人乐趣refreshView(){hintButton.isEnabled = true

    // TODO use forEach with (answer, button) pair and zipped lists
    for((answer, button) in answerList.zip(answerButtonList)){
        button.isEnabled = answer.isEnabled
        button.isSelected = answer.isSelected

        if (answer.isSelected) {
            setButtonColor(button, selectedButtonColor)
        } else {
            setButtonColor(button, defaultButtonColor)
        }
        if (!answer.isEnabled) {
            button.alpha = .5f
            hintButton.isEnabled = false // hint if any answers are hinted
        }
    }
    hintButton.isEnabled = answerList.all { a -> a.isEnabled  }
}

private fun setButtonColor(button: Button, colorString: String) {
    button.backgroundTintList =
        ColorStateList.valueOf(Color.parseColor(colorString))
    button.setTextColor(Color.WHITE)
    button.alpha = 1f
}

}

android kotlin android-toast
1个回答
0
投票

看着您的processSubmitButtonClick(),我认为有两件事是错误的。

首先,您遍历所有答案,然后全部放在isSelected = false上。您将无法告诉用户此后选择的答案!

然后您再次循环,并尝试为存在的每个答案显示一个Toast,您不希望那样。

您可能想做这样的事情:

var correct = false
for (answer in answerList) {
    if (answer.isSelected && answer.isCorrect) {
        correct = true
        break
    }
}

if (correct) {
    //show correct toast
} else {
    //show incorrect toast
}
© www.soinside.com 2019 - 2024. All rights reserved.