我有两个乐谱和一个按钮。我想增加第一个分数,5 秒后增加第二个分数相同的按钮。有代码倒计时器
tt = object : CountDownTimer(5000,1000){
override fun onTick(p0: Long) {
binding.time.text = (p0/1000).toString()
}
override fun onFinish() {
val dialogView = LayoutInflater.from(context).inflate(R.layout.dialogcard,null)
val alert = AlertDialog.Builder(context)
alert.setView(dialogView)
alert.setCancelable(false)
alert.setPositiveButton("Go") { dialogInterface: DialogInterface, i: Int ->
if (!textChanged){
binding.teamName.text = arguments?.getString("teamName2")
// binding.scoreText.text = secondTeamScore.toString()
textChanged = true
} else {
binding.teamName.text = arguments?.getString("teamName1")
// binding.scoreText.text = secondTeamScore.toString()
textChanged = false
}
tt.start()
}.show()
}
}
tt.start()
}
这里的按钮代码:
binding.trueButton.setOnClickListener {
firstTeamScore++
binding.scoreText.text = firstTeamScore.toString()
secondTeamScore++
binding.scoreText.text = secondTeamScore.toString()
}
我该怎么办
可能的解决方案之一是使用 kotlin 协程。示例代码:
CoroutineScope(Dispatchers.Main).async {
delay(5000)
secondTeamScore++
}