我有一个游戏,我使用12个没有文本的按钮来显示生活。每次他们失去生命,这个代码都会运行
public void guessesRemainingDisplay(int numberOfGuesses) {
int guessesRemaining;
guessesRemaining = maximumGuesses + 1 - numberOfGuesses;
switch(guessesRemaining) {
case 1:
findViewById(R.id.Guess1).setBackgroundColor(color.transparent);
break;
case 2:
findViewById(R.id.Guess2).setBackgroundColor(color.transparent);
break;
case 3:
findViewById(R.id.Guess3).setBackgroundColor(color.transparent);
break;
case 4:
findViewById(R.id.Guess4).setBackgroundColor(color.transparent);
break;
case 5:
findViewById(R.id.Guess5).setBackgroundColor(color.transparent);
break;
case 6:
findViewById(R.id.Guess6).setBackgroundColor(color.transparent);
break;
case 7:
findViewById(R.id.Guess7).setBackgroundColor(color.transparent);
break;
case 8:
findViewById(R.id.Guess8).setBackgroundColor(color.transparent);
break;
case 9:
findViewById(R.id.Guess9).setBackgroundColor(color.transparent);
break;
case 10:
findViewById(R.id.Guess10).setBackgroundColor(color.transparent);
break;
case 11:
findViewById(R.id.Guess11).setBackgroundColor(color.transparent);
break;
case 12:
findViewById(R.id.Guess12).setBackgroundColor(color.transparent);
break;
}
}
最右边的按钮消失了(它们排成一行,左边1,右边12)。
然而,当我开始一个新游戏时,或者当活动第一次打开时,此代码将运行
findViewById(R.id.Guess1).setBackgroundColor(color.X);
对每个id重复。X实际上是任何颜色(我已经尝试过很多不同的颜色)。出于某种原因,如果运行此代码,按钮就会消失。为什么?如果它不运行,12个按钮就会出现,但每当我开始一个新游戏时,显然由于失去生命而消失的按钮都不会回来。
对于您的任务,最好使用findViewById(R.id.id).setVisibility(View.Invisible)
来消失,使用'findViewById(R.id.id).setVisibility(View.Visible)'来返回视图。