多测验的 Kotlin Toast
Kotlin Toast for a multi quiz
我是 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()
}
private fun refreshView() {
hintButton.isEnabled = true
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
}
}
看你的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
}
我是 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()
}
private fun refreshView() {
hintButton.isEnabled = true
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
}
}
看你的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
}