检测列表中任何元素的单击(android studio)

问题描述 投票:0回答:1

所以我正在 Android Studio 中开发一个应用程序,我正在制作一个功能,您可以将主图像设置为您按下的任何按钮上的图像。

要检查列表中的任何项目是否被单击,我正在尝试执行类似的操作(不,这不是实际的工作代码,这只是我想要完成的示例):

images.setOnClickListener(clickedImage: ImageButton) { and then set the main image to the image clicked (main image.srcCompat = clickedImage.srcCompat }

PS:这是 Android Studio Hedgehog 中的 Kotlin 语言 PS:我刚刚开始使用 Kotlin,所以这可能只是一个愚蠢的错误,但我不太了解语法。

我猜是正确的(显然是行不通的)是为了列出列表,我应该这样做:

val images = listOf( //list containing every single image button available for investment images
        findViewById<ImageButton>(R.id.imageButton1),
        findViewById<ImageButton>(R.id.imageButton2),
        findViewById<ImageButton>(R.id.imageButton3),
        findViewById<ImageButton>(R.id.imageButton4),
        findViewById<ImageButton>(R.id.imageButton5),
        findViewById<ImageButton>(R.id.imageButton6),
        findViewById<ImageButton>(R.id.imageButton7),
        findViewById<ImageButton>(R.id.imageButton8),
        findViewById<ImageButton>(R.id.imageButton9),
        findViewById<ImageButton>(R.id.imageButton10),
        findViewById<ImageButton>(R.id.imageButton11),
        findViewById<ImageButton>(R.id.imageButton12),
        findViewById<ImageButton>(R.id.imageButton13),
        findViewById<ImageButton>(R.id.imageButton14),
        findViewById<ImageButton>(R.id.imageButton15)
    )

有人知道任何功能或任何方法吗?我基本上只是想检测列表中的任何按钮是否被单击,然后检测它是哪个按钮。

android list kotlin android-imagebutton
1个回答
0
投票

解决方案是for循环。 (我是个白痴哈哈)

© www.soinside.com 2019 - 2024. All rights reserved.