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

1 回答 1

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>
于 2013-04-12T00:29:36.197 回答