在我的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);