我的游戏需要在每个 onCreate 类中更新 TextView 的背景以显示玩家的健康状况,但是目前我能想到的唯一方法是这样
int Health = 100;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act1);
if (Health == 100){
HealthDisplay.setBackgroundResource(R.drawable.health100);
} else if (Health == 99){
HealthDisplay.setBackgroundResource(R.drawable.health99);
} else if (Health == 98){
HealthDisplay.setBackgroundResource(R.drawable.health98);
} else if (Health == 99){
HealthDisplay.setBackgroundResource(R.drawable.health98);
} else if (Health == 99){
HealthDisplay.setBackgroundResource(R.drawable.health98);
} else if (Health == 99){
HealthDisplay.setBackgroundResource(R.drawable.health98);
}
etc.
}
必须有一种更简单/更快的方法来做到这一点,特别是因为我也需要对其他两个统计数据做类似的事情。
我曾考虑让一个单独的类处理它,并在 onCreate 中有一两行告诉它运行该类以更新背景图像,然后返回到这个。
或者,也许这样的事情是可能的?
int Health = 100;
HealthDisplay.setBackgroundResource(R.drawable.health(Health));