不仅如何将按钮的颜色清除为默认颜色,而且还要在我的代码中的什么时刻执行此操作?我已经尝试了一切,但没有运气。当我单击按钮时,我设置了一些具有不透明度的绿色。现在,当我单击“下一步”按钮时,也会发生同样的情况,但第一个按钮仍设置为绿色。我需要它恢复到原来的颜色。 我尝试过:
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;
}
}
}
};
最佳答案
我刚刚制作了一个简单的程序,可以打开和关闭滤光器。
这是 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;
}
这是 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>
关于java - 如何清除按钮的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15960315/