java - 模拟点击后如何使按钮颜色恢复正常
问题描述
在我的 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);
推荐阅读
- azure - 如何找到 Azure 容器注册表映像的所有者
- javascript - 处理具有相同 ID 的引导选项卡
- css - 如何对齐子菜单,与菜单对齐
- c - 构建 i386-jos-elf 时如何消除此 make 错误?
- server - nftables 不允许 ssh
- excel - 在以单元格值为条件的字符串之间复制内容
- firebase - Flutter Authentication signOut - uid 被称为空错误
- elasticsearch - FOS Elastica - 使用连接字段类型
- spring-boot - 找不到在 TLD 函数的方法签名中指定的类 - JasperException
- html - 如何在垂直居中文本后使引导程序中的列 100%?