Toast 没有出现
Toast isn't showing up
尝试显示两个包含一些信息的 Toast,但其中一个未显示。
下面代码中的兴趣点是函数 showScore() 和 checkAnswer()。第一个不显示其 Toast,第二个显示。项目构建成功,应用程序正在我的 phone (Android 9, aarch64) 上运行。
我刚开始学习Android。也许这是一些愚蠢简单的东西,但我不明白为什么它不起作用。
class MainActivity : AppCompatActivity() {
private lateinit var trueButton: Button
private lateinit var falseButton: Button
private lateinit var nextButton: ImageButton
private lateinit var prevButton: ImageButton
private lateinit var questionTextView: TextView
private val questionBank = listOf(
Question(R.string.question_australia, true),
Question(R.string.question_oceans, true),
Question(R.string.question_mideast, false),
Question(R.string.question_africa, false),
Question(R.string.question_americas, true),
Question(R.string.question_asia, true)
)
private val answers = mutableMapOf<Question, Boolean>()
private var currentIndex = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
trueButton = findViewById(R.id.true_button)
falseButton = findViewById(R.id.false_button)
nextButton = findViewById(R.id.next_button)
prevButton = findViewById(R.id.prev_button)
questionTextView = findViewById(R.id.question_text_view)
trueButton.setOnClickListener { view: View ->
checkAnswer(true)
}
falseButton.setOnClickListener { view: View ->
checkAnswer(false)
}
nextButton.setOnClickListener {
currentIndex = (currentIndex + 1) % questionBank.size
updateQuestion()
checkIfAnswered()
}
prevButton.setOnClickListener {
currentIndex = if (currentIndex > 0) (currentIndex - 1) else (questionBank.size - 1)
updateQuestion()
checkIfAnswered()
}
questionTextView.setOnClickListener {
currentIndex = (currentIndex + 1) % questionBank.size
updateQuestion()
checkIfAnswered()
}
updateQuestion()
}
private fun updateQuestion() {
val questionTextResId = questionBank[currentIndex].textResId
questionTextView.setText(questionTextResId)
}
private fun checkAnswer(userAnswer: Boolean) {
val question = questionBank[currentIndex]
val correctAnswer = question.answer
if (!answers.containsKey(question)) {
answers[question] = userAnswer
falseButton.isEnabled = false
trueButton.isEnabled = false
if (answers.size == questionBank.size) {
showScore()
}
} else {
return
}
val messageResId = if (userAnswer == correctAnswer) {
R.string.correct_toast
} else {
R.string.incorrect_toast
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show()
}
private fun checkIfAnswered() {
if (answers.containsKey(questionBank[currentIndex])) {
falseButton.isEnabled = false
trueButton.isEnabled = false
} else {
falseButton.isEnabled = true
trueButton.isEnabled = true
}
}
private fun showScore() {
var score = 0
answers.forEach {
if (it.key.answer == it.value) {
score += 1
}
}
val toastText = "You answered $score of ${questionBank.size}"
Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show()
}
}
UDP:尝试在我朋友的 phone(Android 9,aarch64)上 运行 并遇到同样的问题。
感谢一次 Android 相关的聊天,我发现了问题。问题是一个 Toast 出现在另一个 Toast 之上。通常,以这种方式使用 Toasts 是个坏主意。如果你有类似的问题,可以考虑使用 Snackbar。
尝试显示两个包含一些信息的 Toast,但其中一个未显示。
下面代码中的兴趣点是函数 showScore() 和 checkAnswer()。第一个不显示其 Toast,第二个显示。项目构建成功,应用程序正在我的 phone (Android 9, aarch64) 上运行。
我刚开始学习Android。也许这是一些愚蠢简单的东西,但我不明白为什么它不起作用。
class MainActivity : AppCompatActivity() {
private lateinit var trueButton: Button
private lateinit var falseButton: Button
private lateinit var nextButton: ImageButton
private lateinit var prevButton: ImageButton
private lateinit var questionTextView: TextView
private val questionBank = listOf(
Question(R.string.question_australia, true),
Question(R.string.question_oceans, true),
Question(R.string.question_mideast, false),
Question(R.string.question_africa, false),
Question(R.string.question_americas, true),
Question(R.string.question_asia, true)
)
private val answers = mutableMapOf<Question, Boolean>()
private var currentIndex = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
trueButton = findViewById(R.id.true_button)
falseButton = findViewById(R.id.false_button)
nextButton = findViewById(R.id.next_button)
prevButton = findViewById(R.id.prev_button)
questionTextView = findViewById(R.id.question_text_view)
trueButton.setOnClickListener { view: View ->
checkAnswer(true)
}
falseButton.setOnClickListener { view: View ->
checkAnswer(false)
}
nextButton.setOnClickListener {
currentIndex = (currentIndex + 1) % questionBank.size
updateQuestion()
checkIfAnswered()
}
prevButton.setOnClickListener {
currentIndex = if (currentIndex > 0) (currentIndex - 1) else (questionBank.size - 1)
updateQuestion()
checkIfAnswered()
}
questionTextView.setOnClickListener {
currentIndex = (currentIndex + 1) % questionBank.size
updateQuestion()
checkIfAnswered()
}
updateQuestion()
}
private fun updateQuestion() {
val questionTextResId = questionBank[currentIndex].textResId
questionTextView.setText(questionTextResId)
}
private fun checkAnswer(userAnswer: Boolean) {
val question = questionBank[currentIndex]
val correctAnswer = question.answer
if (!answers.containsKey(question)) {
answers[question] = userAnswer
falseButton.isEnabled = false
trueButton.isEnabled = false
if (answers.size == questionBank.size) {
showScore()
}
} else {
return
}
val messageResId = if (userAnswer == correctAnswer) {
R.string.correct_toast
} else {
R.string.incorrect_toast
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show()
}
private fun checkIfAnswered() {
if (answers.containsKey(questionBank[currentIndex])) {
falseButton.isEnabled = false
trueButton.isEnabled = false
} else {
falseButton.isEnabled = true
trueButton.isEnabled = true
}
}
private fun showScore() {
var score = 0
answers.forEach {
if (it.key.answer == it.value) {
score += 1
}
}
val toastText = "You answered $score of ${questionBank.size}"
Toast.makeText(this, toastText, Toast.LENGTH_SHORT).show()
}
}
UDP:尝试在我朋友的 phone(Android 9,aarch64)上 运行 并遇到同样的问题。
感谢一次 Android 相关的聊天,我发现了问题。问题是一个 Toast 出现在另一个 Toast 之上。通常,以这种方式使用 Toasts 是个坏主意。如果你有类似的问题,可以考虑使用 Snackbar。