如何清除按钮的颜色?

时间:2013-04-11 22:59:13

标签: java android

不仅如何将颜色清除为我的按钮的默认颜色,还要在我的代码中的哪一刻做到这一点?我已经尝试了一切,但没有运气。当我点击一个按钮时,我设置了一些不透明的绿色。现在,当我单击下一个按钮时,同样的情况发生但第一个按钮仍然设置为绿色。我需要它恢复到原始颜色。 我尝试过:

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;
              }
           }
               }       
           };

1 个答案:

答案 0 :(得分: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>