如何在模拟点击后让按钮颜色恢复正常
How to get button color back to normal after a simulated click
在我的 Android 应用程序中,我使用以下代码模拟按钮点击:
void clickButton(final Button b, int delay)
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
final Drawable drawable = b.getBackground();
b.performClick();
b.getBackground().setColorFilter(b.getContext().getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
// b.setBackgroundColor(Color.rgb(88, 166, 198));
b.setPressed(true);
b.invalidate();
// delay completion till animation completes
b.postDelayed(new Runnable() { //delay button
public void run() {
b.setPressed(false);
b.invalidate();
b.setBackground(drawable);
//any other associated action
}
}, 800); // .8secs delay time
}
}, delay);
}
但是点击后按钮的颜色会保持绿色,如何在延迟 0.5 秒后恢复为点击前的颜色?
b.setBackgroundColor(ContextCompat.getColor(b.getContext(), R.color.colorAccent));
b.postDelayed(new Runnable() {
public void run() {
b.setBackgroundColor(ContextCompat.getColor(b.getContext(), R.color.initialColor));
}
}, 800);
在我的 Android 应用程序中,我使用以下代码模拟按钮点击:
void clickButton(final Button b, int delay)
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
final Drawable drawable = b.getBackground();
b.performClick();
b.getBackground().setColorFilter(b.getContext().getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
// b.setBackgroundColor(Color.rgb(88, 166, 198));
b.setPressed(true);
b.invalidate();
// delay completion till animation completes
b.postDelayed(new Runnable() { //delay button
public void run() {
b.setPressed(false);
b.invalidate();
b.setBackground(drawable);
//any other associated action
}
}, 800); // .8secs delay time
}
}, delay);
}
但是点击后按钮的颜色会保持绿色,如何在延迟 0.5 秒后恢复为点击前的颜色?
b.setBackgroundColor(ContextCompat.getColor(b.getContext(), R.color.colorAccent));
b.postDelayed(new Runnable() {
public void run() {
b.setBackgroundColor(ContextCompat.getColor(b.getContext(), R.color.initialColor));
}
}, 800);