ImageButton-如何读取图像ID?

发布于 2025-02-11 21:57:35 字数 366 浏览 1 评论 0原文

对不起,这个问题的问题,但我是Android的新手。我有一个问题。我有几个imageButton元素,我需要根据OnClick()事件中的内容来更改它们的图像。虽然设置新图像不是问题,但我无法编程地读取当前的图像(下一个图像取决于它)。我搜索各种来源并且找不到解决方案。当然很简单。

我想拥有这样的代码:

if (ImageButton1.image... == R.drawable.img1)
{
   ImageButton1.setImageResource(R.drawable.img2)
   ImageButton2.setImageResource(R.drawable.img3)
   ...
}

任何帮助。谢谢...

Sorry for the banal question, but I'm new to Android. I have such a problem. I have several ImageButton elements and I need to change their images depending on what is on them during onClick() event. While setting a new image is not a problem, I can not programmatically read what image is currently (the next image depends on it).I search various sources and can't find a solution. It is certainly very simple.

I want to have a code like this:

if (ImageButton1.image... == R.drawable.img1)
{
   ImageButton1.setImageResource(R.drawable.img2)
   ImageButton2.setImageResource(R.drawable.img3)
   ...
}

Any help appreciated. Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2025-02-18 21:57:35

解决此问题的一种方法是有一个int数组,并以格式存储在

var listOfImages = listOf<Int>(R.drawable.one,R.drawable.two)

数组

One approach to get over this is have an Int array and store the drawables in the array in the format

var listOfImages = listOf<Int>(R.drawable.one,R.drawable.two)

Now set the images as ImageButton1.setImageResource(listOfImages(0))

That way you know which drawablr is being used

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文