如何使imageView背景更改一次

时间:2018-11-18 18:47:52

标签: android kotlin

if (buttonClicked.contains(1) && buttonClicked.contains(2)) {
    if (playerOneLastClicked) {
        imgViewBackground1.setImageResource(R.drawable.rca)
    } else {
        imgViewBackground1.setImageResource(R.drawable.wac)
    }
}

if (buttonClicked.contains(3) && buttonClicked.contains(4) && buttonClicked.contains(5)) {
    if (playerOneLastClicked) {
        imgViewBackground2.setImageResource(R.drawable.rca)
    } else {
        imgViewBackground4.setImageResource(R.drawable.wac)
    }
}

player1 player2 上次单击(buttonClicked.contains(1) && buttonClicked.contains(2)) imgViewBackgound1时发生更改,但是当玩家单击按钮3,4或5 imgViewBackground时保持更改。< / p>

我希望imgViewBackground1(R.drawable.rca)(R.drawable.wac)时不再更改。

1 个答案:

答案 0 :(得分:0)

如果imgViewBackground不会显示图片,则可以添加一个额外的支票,例如

if (imgViewBackground1.drawable == null) {
     if (playerOneLastClicked) {
                imgViewBackground1.setImageResource(R.drawable.rca)
            } else {
                imgViewBackground1.setImageResource(R.drawable.wac)
            }
    }
}

或者如果它已经有图像视图并且您不想再次设置它

if (imgViewBackground1.drawable != resources.getDrawable(R.drawable.rcs, null) 
    || imgViewBackground1.drawable != resources.getDrawable(R.drawable.wac, null))
    if (playerOneLastClicked) {
                    imgViewBackground1.setImageResource(R.drawable.rca)
                } else {
                    imgViewBackground1.setImageResource(R.drawable.wac)
                }
    }
}