android studio中按钮的图像
这是我的按钮类实现:android studio中按钮的图像,android,kotlin,Android,Kotlin,这是我的按钮类实现: package com.example.myproject import android.R import android.content.Context import android.graphics.Color import android.os.CountDownTimer import android.util.Log import android.widget.ImageView import kotlin.properties.Delegates data
package com.example.myproject
import android.R
import android.content.Context
import android.graphics.Color
import android.os.CountDownTimer
import android.util.Log
import android.widget.ImageView
import kotlin.properties.Delegates
data class MyButton() : androidx.appcompat.widget.AppCompatButton(context) {
init {
this.text = "test"
this.setOnClickListener { v ->
Log.d("CLICK", "click")
}
object: CountDownTimer(_time, 1000) {
override fun onTick(millisUntilFinished: Long) {
var rnd = (0..20).random()
if (rnd < 10) {
this.setBackgroundResource(R.drawable.notebook) //notebook
} else {
this.setBackgroundResource(R.drawable.desktop_pc) //desktop PC
}
}
override fun onFinish() {
exitProcess(-1)
}
}.start()
}
}
package com.example.myproject
导入android.R
导入android.content.Context
导入android.graphics.Color
导入android.os.CountDownTimer
导入android.util.Log
导入android.widget.ImageView
导入kotlin.properties.Delegates
数据类MyButton():androidx.appcompat.widget.AppCompatButton(上下文){
初始化{
this.text=“测试”
this.setOnClickListener{v->
Log.d(“单击”、“单击”)
}
对象:倒计时(_time,1000){
覆盖趣味点击(毫秒直到完成:长){
var rnd=(0..20).random()
如果(rnd<10){
this.setBackgroundResource(R.drawable.notebook)//notebook
}否则{
this.setBackgroundResource(R.drawable.desktop\u pc)//桌面pc
}
}
重写函数onFinish(){
退出流程(-1)
}
}.start()
}
}
所以我想,基于一些随机生成器,把笔记本电脑图像或台式电脑图像放在我的按钮上。这是我的res文件夹:
res>drawable>notebook.png,desktop\u pc.png
这两张图片我都是用复制粘贴的。但问题是R.drawable方法找不到这两个文件,无法从此文件夹加载它们。问题出在哪里?我认为您的R导入是错误的,您使用的是android.R,但应该是您的“应用程序包.R”,请更改它