[英]I Can't change the Button's color onClick in Android Studio by Kotlin
I can't change the button color when I pressed on it after using onClick event, it still has the same color.在使用 onClick 事件后按下按钮颜色时,我无法更改按钮颜色,它仍然具有相同的颜色。 the kotlin code below..下面的 kotlin 代码..
fun buttonSelect(view:View){
val buSelect=view as Button
var cellID=0
when(buSelect.id){
R.id.bu11 -> cellID=1
R.id.bu12 -> cellID=2
R.id.bu13 -> cellID=3
R.id.bu21 -> cellID=4
R.id.bu22 -> cellID=5
R.id.bu23 -> cellID=6
R.id.bu31 -> cellID=7
R.id.bu32 -> cellID=8
R.id.bu33 -> cellID=9
}
Log.d("Cell id: ",cellID.toString())
buSelect.setBackgroundResource(R.color.black)
}
} }
You should use onClickListener你应该使用 onClickListener
myButton.setOnClickListener {
myButton.setBackgroundColor(R.color.black)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.