gpt4 book ai didi

java - 更改 Android 应用程序中按钮的背景颜色

转载 作者:行者123 更新时间:2023-11-30 09:44:19 25 4
gpt4 key购买 nike

我想在单击按钮时更改按钮的背景颜色。我的目标是颜色应该改变 5 秒,然后再次改变为另一种颜色。

按钮的原始颜色是黄色。

这是我试过的部分代码:

public void click(View view){
myTestButton = (Button)view;
myTestButton.setBackgroundColor(Color.BLUE);
//*Wait lines;*
myTestButton.setBackgroundColor(Color.RED);
}

按钮将颜色变为红色,但不会变为蓝色。我怀疑该 View 直到稍后才刷新。我希望在等待行之前刷新按钮。我也尝试过 myTestButton.invalidate() 但无济于事。

在此先感谢您提供的一些重要提示!!

最佳答案

您在“等待队列”中使用什么?我猜那里有问题,因为您可能不会让您的 UI 线程在那里 hibernate ,并且此方法 (onClick) 由您的 UI 线程调用。

我建议您使用方法 View.postDelayed(Runnable action, long delayMills 来做到这一点。示例:

myTestButton.postDelayed(new Runnable() {
public void run() {
myTestButton.setBackgroundColor(Color.RED);
}
}

请注意,您必须在 onClick 方法中将 myTestButton 声明为 final

关于java - 更改 Android 应用程序中按钮的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7957494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com