[英](Android Studio, Kotlin) Image in Imageview is not displaying when clicked
Hi all I have a problem in displaying image in ImageView when clicked, i made a function to display whenever the image was click I have already added the android:onClick="drops" in .xml in every imageview of my sample game, i used gridlayout(3x3) with 9 imageview大家好我在点击时显示在ImageView的形象问题,我犯了一个功能显示器每当形象是点击我已经添加了android:的onClick =“滴”为.xml在我的样本游戏的每一个imageview的,我用gridlayout(3x3) 带有 9 个图像视图
here is the code of the function.这是函数的代码。
fun drops(view: View){
val imageView = ImageView(this)
var player = 0;
if (player == 0){
imageView.setImageResource(R.drawable.yellow)
imageView.animate().alpha(1f).rotation(360f).setDuration(600)
player == 1
}else {
imageView.setImageResource(R.drawable.red)
imageView.animate().alpha(1f).rotation(360f).setDuration(600)
player == 0
}
}
I'm unsure in what class is this function contained, I suppose it is either in a Fragment
or Activity
.我不确定这个函数包含在哪个类中,我想它要么在
Fragment
要么在Activity
。 As you said, you're binding the image views with drops
method in the xml layout.正如您所说,您正在使用 xml 布局中的
drops
方法绑定图像视图。
What could be the problem is this line:这行可能是什么问题:
val imageView = ImageView(this)
From your question we cannot see which class is this method defined in, so expression this
could be anything.从您的问题中,我们无法看到此方法定义在哪个类中,因此表达式
this
可以是任何内容。 You might me instantiating an ImageView
from the whole Activity or Fragment object, and cannot work in way you would like to.您可能会从整个 Activity 或 Fragment 对象实例化一个
ImageView
,但无法按您希望的方式工作。 So I suggest to rewrite this line to something like this:所以我建议将这一行改写成这样:
val imageView = view as ImageView
The function drops
receives a single View
parameter, which is the image view the user clicked - but View
class has no method called setImageResource
, so you need to cast it to the desired subtype (ImageView is subclass of View). drops
函数接收单个View
参数,即用户单击的图像视图 - 但View
类没有名为setImageResource
方法,因此您需要将其转换为所需的子类型(ImageView 是 View 的子类)。 This should do the job.这应该可以完成工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.