你可以使用按钮的状态来改变你想要的颜色。对于这种方法,您需要创建一个可绘制的 xml 文件。像这样的东西(来自我的一个项目的例子):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<solid android:color="@color/transparent_button_ripple_color"/>
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<solid android:color="@color/transparent_button_ripple_color"/>
</shape>
</item>
<item android:state_enabled="false">
<shape android:shape="rectangle" >
<solid android:color="@color/transparent_button_ripple_color"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<gradient android:angle="-90" android:startColor="@color/colorTransparent" android:endColor="@color/colorTransparent" />
</shape>
</item>
</selector>
他们通过设置按钮的状态来控制颜色。
此外,为了在 android 中表示点击,您可以添加涟漪效应(仅供参考)。 https://guides.codepath.com/android/ripple-animation
已编辑
我查看了您之前的问题并针对您的代码创建了解决方案:
创建这个方法:
private void updateViewColorWithDelay(View targetView) {
targetView.setBackgroundColor(Color.parseColor("#000000"));
targetView.postDelayed(new Runnable() {
@Override
public void run() {
ivAvatar.setBackgroundColor(Color.parseColor("#ffffff"));
}
},300);
}
并将其与您的视图一起使用:
private class AutoDemoListener implements View.OnClickListener {
public void onClick(View v) {
Is_AutoDemo_B=true;
Out("AutoDemoListener");
switchView(demoView, registrationView);
startRegistration();
final Handler handler = new Handler();
registrationView.symbolButton[2][8].performClick();
updateViewColorWithDelay(registrationView.symbolButton[[2][8]);
handler.postDelayed(new Runnable() {
public void run() {
registrationView.symbolButton[4][13].performClick();
updateViewColorWithDelay(registrationView.symbolButton[4][13]);
}
}, 1000);
handler.postDelayed(new Runnable() {
public void run() {
registrationView.symbolButton[0][1].performClick();
updateViewColorWithDelay(registrationView.symbolButton[0][1]);
}
}, 3000);
handler.postDelayed(new Runnable() {
public void run() {
registrationView.symbolButton[6][18].performClick();
updateViewColorWithDelay(registrationView.symbolButton[6][18]);
}
}, 5000);
handler.postDelayed(new Runnable() {
public void run() {
Is_AutoDemo_B=false;
}
}, 5100);
}
}