如何重置以前点击按钮的颜色?

时间:2013-04-09 16:13:35

标签: java android

我在onClickListener中有这个代码(我还有别的东西,但它与这个问题无关:

final OnClickListener clickListener = new OnClickListener() {


            public void onClick(View v) {

                Button button = (Button) v;
                button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));

}
}

按下它时,我将按钮颜色设置为绿色(带有一些不透明度)。如何清除该颜色,将其重置为我的默认按钮颜色(不是Android默认设置,我的默认设置颜色),并且只有当前按下的按钮为绿色?

编辑:

这是我的整个onClickListener代码:

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 () != buttonClicked.getParent()) {
                    // second button is clicked
                    if (buttonClicked.getTag().equals(button.getTag())) {
                        Toast.makeText(Spojnice.this, "Tacno", Toast.LENGTH_SHORT).show();
                        button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
                        buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
                        buttonClicked.setEnabled(false);
                        button.setEnabled(false);
                    } else {
                        Toast.makeText(Spojnice.this, "Netacno", Toast.LENGTH_SHORT).show();
                        //buttonClicked.setEnabled(false);
                        //buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFCC99));
                        //button.getBackground().clearColorFilter();
                    }
                    buttonClicked = null;
                }       
            }
     };

2 个答案:

答案 0 :(得分:1)

像按钮背景一样使用smth:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:drawable="@drawable/drawable1" /> -- when button is pressed
     <item android:drawable="@drawable/drawable2" /> -- button isn't pressed
 </selector>

答案 1 :(得分:1)

使用:

button.getBackground().setColorFilter(null);

删除滤色镜。