简体   繁体   中英

How to clear color of a button?

Not only how to clear color to default color of my button, but also in what moment in my code to do it? I've tried everything but no luck. When I click a button I set some green color with opacity. Now when I click next button the same happens but the first button is still set to green. I need it to revert to original color. I tried with:

button.getBackground().setColorFilter(null);

Here's my code:

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

I just made a simple program which toggles the light filter on and off.

Here is the Activity:

 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;

     }

and here is the 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM