2012-01-14 96 views
0

我有一個遊戲,我使用12個按鈕,沒有文字顯示生活。每當他們失去了生命,這個代碼運行製作一個按鈕透明,然後在Android中再次不透明

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個按鈕,但每當我開始一個新遊戲時,顯然由於失去生命而消失的按鈕不會再回來。

回答

1

對於你的任務,最好使用findViewById(R.id.id).setVisibility(View.Invisible)消失,'findViewById(R.id.id).setVisibility(View.Visible)'返回視圖。

+0

謝謝!這工作完美 – 2012-01-14 14:49:08