2013-04-11 74 views
0

不僅如何將顏色清除爲我按鈕的默認顏色,而且還在我的代碼的哪一刻執行此操作?我嘗試了一切,但沒有運氣。當我點擊一個按鈕時,我設置了一些不透明的綠色。現在,當我點擊下一個按鈕時,同樣的情況發生,但第一個按鈕仍然設置爲綠色。我需要它恢復到原來的顏色。 我試着用:如何清除按鈕的顏色?

button.getBackground().setColorFilter(null); 

這裏是我的代碼:

final OnClickListener clickListener = new OnClickListener() { 

      private Button buttonClicked; 

      public void onClick(View v) { 
       Button button = (Button) v; 
       button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333)); 

       if (buttonClicked == null) { 
        // first button is clicked 
        buttonClicked = button; 
        // only do stuff if buttons are in different layouts 
       } else{ 
      if (!button.getParent().equals(buttonClicked.getParent())) { 
       // second button is clicked 

      if(buttonClicked.getTag().equals(button.getTag())){ 

       // second button is clicked and same tag but different button 

       Toast.makeText(Spojnice.this, "Correct", Toast.LENGTH_SHORT).show(); 
       button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33)); 
       buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33)); 
       buttonClicked.setEnabled(false); 
       button.setEnabled(false); 
       buttonClicked = null; 
       } else { 
       //reset LightingColorFilter first 
       Toast.makeText(Spojnice.this, "Wrong", Toast.LENGTH_SHORT).show(); 
       buttonClicked = null; 

       } 
       }else{ 

        buttonClicked = button; 
       } 
      } 
       }  
      }; 
+0

類似的問題在這個職位回答。請檢查 [清除按鈕顏色] [1] [1]:http://stackoverflow.com/questions/15704898/how-to-clear-buttons-color-when-next-按鈕被點擊?rq = 1 –

+0

我看到了,並嘗試過,但沒有奏效。 – marjanbaz

回答

1

我只是做了一個簡單的程序,切換和關閉濾光器。

這裏的活動:

Button buttonClicked = null; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    public void clickedButton(View v) { 
     Button button = (Button)v; 
     button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 
                     0x66FF33)); 

     if (buttonClicked != null) { 
      buttonClicked.getBackground().setColorFilter(null); 
     } 
     buttonClicked = button; 

    } 

這裏是XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
     > 
    <TextView 
      android:id="@+id/boss" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Hello World, MyActivity" 
      /> 
    <Button 
      android:id="@+id/buttsky" 
      android:layout_below="@id/boss" 
      android:onClick="clickedButton" 
      android:layout_width="200dp" 
      android:layout_height="100dp" 
      android:text="pushMe" 
      /> 
    <Button 
      android:id="@+id/buttground" 
      android:layout_below="@id/buttsky" 
      android:onClick="clickedButton" 
      android:layout_width="200dp" 
      android:layout_height="100dp" 
      android:text="no, pushMe" 
      /> 
</RelativeLayout> 
+0

我得到錯誤「方法getColorFilter未定義爲Drawable類型」。 – marjanbaz

+0

拍攝,我從ImageView源拉,雖然這將工作。我會更密切地看待它。 – HalR

+0

這裏的主要問題是,我不知道在第二次單擊時將清除第一次單擊按鈕的代碼置於何處。 – marjanbaz