1

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

2 回答 2

1

使用 smth like 作为按钮背景:

 <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>
于 2013-04-09T16:16:05.053 回答
1

利用:

button.getBackground().setColorFilter(null);

以移除滤色器。

于 2013-04-09T16:17:48.187 回答