英文:
How do I make the image change automatically after a certain time?
问题
以下是您要翻译的代码部分:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.nav_sair -> {
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
finish()
Toast.makeText(this, "Logue novamente", Toast.LENGTH_LONG).show()
true
}
R.id.nav_sobre -> {
binding.imvTroca.setImageResource(R.drawable.perfectaprint_quemsomos_full_editado)
true
}
R.id.nav_impressoras -> {
binding.imvTroca.setImageResource(R.drawable.imagem_impressoras_edit)
true
}
R.id.nav_produtos_materiais -> {
binding.imvTroca.setImageResource(R.drawable.produtos_materiais)
true
}
R.id.nav_fornecedores -> {
binding.imvTroca.setImageResource(R.drawable.fornecedores)
true
}
else -> {
super.onOptionsItemSelected(item)
}
}
}
With each click on one of the menus, it brings a new image, I wanted it to return after a while to the default image.
英文:
With each click on one of the menus, it brings a new image, I wanted it to return after a while to the default image.
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.nav_sair -> {
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
finish()
Toast.makeText(this, "Logue novamente", Toast.LENGTH_LONG).show()
true
}
R.id.nav_sobre -> {
binding.imvTroca.setImageResource(R.drawable.perfectaprint_quemsomos_full_editado)
true
}
R.id.nav_impressoras -> {
binding.imvTroca.setImageResource(R.drawable.imagem_impressoras_edit)
true
}
R.id.nav_produtos_materiais -> {
binding.imvTroca.setImageResource(R.drawable.produtos_materiais)
true
}
R.id.nav_fornecedores -> {
binding.imvTroca.setImageResource(R.drawable.fornecedores)
true
}
else -> {
super.onOptionsItemSelected(item)
}
}
}
}
With each click on one of the menus, it brings a new image, I wanted it to return after a while to the default image.enter code here
答案1
得分: 0
你可以使用协程,如果它被中断,你可以替换它。
private val imvTrocaImageChangeJob: Job? = null
private fun setTemporaryImvTrocaImage(imageResId: Int) {
imvTrocaImageChangeJob?.cancel() // 中断任何现有的图像更改任务
imvTrocaImageChangeJob = lifecycleScope.launch {
binding.imvTroca.setImageResource(imageResId)
delay(2000L) // 2000 毫秒
binding.imvTroca.setImageResource(R.drawable.the_original_image)
}
}
// ...
R.id.nav_produtos_materiais -> {
setTemporaryImvTrocaImage(R.drawable.produtos_materiais)
true
}
// 等等...
英文:
You can use a coroutine that you can replace if it gets interuppted.
private val imvTrocaImageChangeJob: Job? = null
private fun setTemporaryImvTrocaImage(imageResId: Int) {
imvTrocaImageChangeJob?.cancel() // interrupt any existing image change job
imvTrocaImageChangeJob = lifecycleScope.launch {
binding.imvTroca.setImageResource(imageResId)
delay(2000L) // 2000 milliseconds
binding.imvTroca.setImageResource(R.drawable.the_original_image)
}
}
// ...
R.id.nav_produtos_materiais -> {
setTemporaryImvTrocaImage(R.drawable.produtos_materiais)
true
}
// etc. ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论