2011-11-05 60 views
0

此刻我的按鈕不起作用。前兩次被按下的所有按鈕都會受到影響,而不僅僅是被按下的按鈕。爲什麼setAlpha()作用於我的所有按鈕,而setImageResource()只作用於一個按鈕?

交換:

seatButton[i].setAlpha(255); 

爲:

seatButton[i].setImageResource(0x7f020007) 

我的代碼工作!只有我按下的按鈕纔會生效。爲什麼?

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    table = new Table(); //Creates Table 
    seatButton = new ImageButton[10]; //Creates Array of buttons 
    seatStats = new TextView[10]; //Creates array for stat panels 


    //Creates longClickListener, used for players to sit in or out. 
    longClickListener = new View.OnLongClickListener() 


    { 


     @Override 
     public boolean onLongClick(View v) 
     { 
      for(int i=0; i<10; i++) 
      { 
       //Each seat[i] will correspond with each imageButtoni+1 
       if(v.getId() == (getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud"))) 
       { 
        //If the seat is empty fill it, place a player in the seat and change the button from translucent to opaque 
        if(table.seats[i].getState().equals("empty")) 
        { 

         seatButton[i].setAlpha(255); 
         //seatButton[i].setImageResource(0x7f020000); 
         table.seats[i].sit(new Player()); 
         seatStats[i].setVisibility(View.VISIBLE); 
         Toast.makeText(GUI.this, table.seats[i].getState(), Toast.LENGTH_SHORT).show(); 
        } 
        //If the seat is full, empty it 
        else 
        { 
         seatButton[i].setAlpha(80); 
         //seatButton[i].setImageResource(0x7f020007); 
         table.seats[i].sitOut(); 
         seatStats[i].setVisibility(View.INVISIBLE); 
         Toast.makeText(GUI.this, table.seats[i].getState() + i, Toast.LENGTH_SHORT).show(); 
        } 
       } 
      } 
      return true; 
     } 
    }; 


    //Assigns the buttons and stats panels defined in the layout xml to their appropiate java arrays. Also sets clickListeners to buttons. 
    for(int i = 0; i < 10; i++) 
    { 
     seatButton[i] = (ImageButton) findViewById(getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud")); 
     seatStats[i] = (TextView) findViewById(getResources().getIdentifier("textView" + (i+1), "id", "en.deco.android.livehud")); 
     seatButton[i].setOnLongClickListener(longClickListener); 
     seatButton[i].setAlpha(80); 
    } 

回答

1

您正在與==進行字符串比較,這意味着您正在比較引用而不是值。這可能不是你想要的,所以你應該改變,從:

if(table.seats[i].getState() == "empty") { ... } 

到:

if(table.seats[i].getState().equals("empty")) { ... } 

除此之外,根據setAlpha(float alpha)文檔(這是一個API 11的方法,只是參考),通過的浮動應該在[0...1]之間。

您設置的圖像資源是ImageManager的R.id.transparent_background。這可能表明你的邏輯有效,但錯誤確實在設置alpha值的某處。

+0

感謝這確實是一個錯誤,儘管按鈕的前兩次點擊仍然存在問題。 – Deco

+0

用可能不正確的setAlpha調用擴展我的答案。 –

+0

Alpha值是從ImageView/ImageButtons的0..255開始的整數。我最後一次嘗試涉及用Alpha動畫替換setAlpha。不幸的是這也失敗了。第一次點擊會很好,但第二次會影響所有按鈕。開始認爲Alpha方法本身可能有點狡猾。 – Deco

0

我找到了一個解決方案,但我不明白它,我也不滿意。我設置了一個alpha動畫,在打開我的程序時將我的所有按鈕的alpha值從255改爲255。換句話說,它什麼都不做。但是我的程序現在可以運行我希望有一個更好的解決方案或解釋爲什麼這個工作?

此代碼與onCreate()方法開始時的其餘初始化一起放置。它設置了一個保持相同值的AlphaAnimation。

//Initializes AlphaAnimations to alter transparency 
alphaDown = new AlphaAnimation(1f, 1f); 
alphaDown.setDuration(1000); 
alphaDown.setFillAfter(true); 

這是與我的問題的底部顯示的相同的循環,改變了一行。它會在將所有按鈕設置爲半透明之前激活此靜態動畫。沒有這個動畫,所有按鈕都會受到一次點擊的影響使用動畫時,每個按鈕都會響應並且只有它被點擊。

 //Assigns the buttons and stats panels defined in the layout xml to their appropiate java arrays. Also sets clickListeners to buttons. 
    for(int i = 0; i < 10; i++) 
    { 
     seatButton[i] = (ImageButton) findViewById(getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud")); 
     seatStats[i] = (TextView) findViewById(getResources().getIdentifier("textView" + (i+1), "id", "en.deco.android.livehud")); 
     seatButton[i].setOnLongClickListener(longClickListener); 
     seatButton[i].startAnimation(alphaDown); 
     seatButton[i].setAlpha(80); 
    } 
+0

這是一個臨時解決方案,沒有解決。後來我遇到類似的問題,點擊完全不相關的按鈕會顛倒透明度。 – Deco

相關問題