Android Kotlin 安卓 根据图片判断点击按钮后切换成什么图片

几天前做的,具体内容不太记得了。

简单来说就是当我有n张图片,我希望按一下按钮能切换。同时,当显示的图片是某一张特定图片时,我希望能够在按下按钮之后跳转到另外一张由我指定的图片。

即:我需要判断当前正在显示的图片是否为我需要的图片。

询问ChatGPT得到以下结果:

package com.example.uiwidgettest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView

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

        val button: Button = findViewById(R.id.button)
        val imageView: ImageView = findViewById(R.id.xxx1)

        button.setOnClickListener {
            // 获取当前的图片资源
            val currentDrawable = imageView.drawable

            // 判断当前的图片资源是否为 R.drawable.xxx1
            if (currentDrawable.constantState == resources.getDrawable(R.drawable.xxx1).constantState) {
                // 如果是,则切换到 R.drawable.xxx2
                imageView.setImageResource(R.drawable.xxx2)
            } else {
                // 否则,切换回 R.drawable.xxx1
                imageView.setImageResource(R.drawable.xxx1)
            }
        }
    }
}

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下步骤来实现切换音乐更换音乐图片的功能: 1. 创建一个包含所有歌曲信息的数据类,包括歌曲名称、歌手、时长和音乐图片等信息。 ```kotlin data class Song(val name: String, val artist: String, val duration: Int, val image: Int) ``` 2. 在主活动中创建一个包含所有歌曲的列表,并初始化当前播放歌曲的索引。 ```kotlin val songs = listOf( Song("song1", "artist1", 180, R.drawable.image1), Song("song2", "artist2", 240, R.drawable.image2), Song("song3", "artist3", 300, R.drawable.image3) ) var currentSongIndex = 0 ``` 3. 在布局文件中添加一个ImageView控件用于显示当前播放歌曲的图片,并在活动中找到该控件。 ```xml <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/image1" /> ``` ```kotlin val imageView = findViewById<ImageView>(R.id.imageView) ``` 4. 在活动中创建一个函数用于切换音乐和图片,该函数接受一个整数参数,表示要切换到的歌曲的索引。 ```kotlin private fun switchSong(index: Int) { // 检查索引是否越界 if (index < 0 || index >= songs.size) { return } // 更新当前播放歌曲的索引 currentSongIndex = index // 获取当前播放歌曲的信息 val currentSong = songs[currentSongIndex] // 更新音乐图片 imageView.setImageResource(currentSong.image) // 切换音乐 // TODO: 实现切换音乐的逻辑 } ``` 5. 在布局文件中添加两个按钮控件,一个用于切换到上一首歌曲,一个用于切换到下一首歌曲,并在活动中找到这两个控件。 ```xml <Button android:id="@+id/prevButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Prev" /> <Button android:id="@+id/nextButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" /> ``` ```kotlin val prevButton = findViewById<Button>(R.id.prevButton) val nextButton = findViewById<Button>(R.id.nextButton) ``` 6. 在活动的onCreate函数中为两个按钮添加点击事件处理程序,分别调用switchSong函数切换上一首或下一首歌曲。 ```kotlin override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // 初始化imageView和prevButton、nextButton prevButton.setOnClickListener { switchSong(currentSongIndex - 1) } nextButton.setOnClickListener { switchSong(currentSongIndex + 1) } } ``` 完以上步骤后,你就可以在你的应用程序中切换音乐并更换音乐图片了。注意,实现切换音乐的逻辑需要根据你的具体需求进行编写。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值