我尝试了很多东西,但是不知道在if / when语句的括号里放什么。
var y = getResources()。getIdentifier(“ menu”,“ drawable”,getPackageName())
var y1 = getResources().getIdentifier("door1", "drawable", getPackageName())
var y2 = getResources().getIdentifier("door_inside", "drawable", getPackageName())
var y3 = getResources().getIdentifier("door_back", "drawable", getPackageName())
buttonYes.setOnClickListener {
when (y) {
y -> imageView.setImageResource(R.drawable.door1)
}
if (y1 = true) {
imageView.setImageResource(R.drawable.door_inside)
}
/*
when (y2) {
y2 -> imageView.setImageResource(R.drawable.door_inside2)
}
when (y3) {
y3 -> imageView.setImageResource(R.drawable.door_back2)
}
*/
}
var n = getResources().getIdentifier("menu", "drawable", getPackageName())
var n1 = getResources().getIdentifier("door1", "drawable", getPackageName())
var n2 = getResources().getIdentifier("door_back", "drawable", getPackageName())
var n3 = getResources().getIdentifier("door_inside", "drawable", getPackageName())
buttonNo.setOnClickListener {
if (imageView.resources = n) {
finishAffinity();
}
if (drawable=n1) {
imageView.setImageResource(R.drawable.door_back)
}
/*
when(n2) {
n2 -> imageView.setImageResource(R.drawable.door_back2_1)
}
when(n3) {
n3 -> imageView.setImageResource(R.drawable.door_inside2_1)
}
*/
}
}
}
答案 0 :(得分:0)
您是否正在寻找类似的东西?
string buttonSequense ="";
buttonYes.setOnClickListener {
buttonSequense +="yes";
setImage();
}
buttonNo.setOnClickListener {
buttonSequense +="no";
setImage();
}
然后使用setImage方法:
public void setImage(){
if(buttonSequense.equals("yes")){
imageView.setImageResource(R.drawable.door_inside)
}else if(buttonSequense.equals("yesno")){
imageView.setImageResource(R.drawable.door_back)
}else if(buttonSequense.equals("yesnonoyesno"))
//... and so on
}
答案 1 :(得分:0)
似乎可以绘制可绘制对象的地图并使用它。
import your.package.name.R.drawable.*
//...
data class Choice(val choice1: Int, val choice2: Int)
val choicesMap = mapOf(
door1 to Choice(door_inside, door_back),
door_inside to Choice(door_further_inside, door1),
//etc.
)
然后在应用它时存储当前Drawable并使用它进行选择。
var currentImageDrawable = R.drawable.door1
//...
buttonYes.setOnClickListener {
currentImageDrawable = choicesMap[currentImageDrawable].choice1
imageView.setImageResource(currentImageDrawable)
}
buttonNo.setOnClickListener {
currentImageDrawable = choicesMap[currentImageDrawable].choice2
imageView.setImageResource(currentImageDrawable)
}