gpt4 book ai didi

android - 在 Android 中,如何更改按钮的颜色,暂停几秒钟,然后再次更改?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:04:10 26 4
gpt4 key购买 nike

这是我的代码:

public class TestActivity extends Activity {    

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
start();
}

private void start() {
setContentView(R.layout.main);

LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);

Button button = new Button(this);

layout.addView(button);

button.getBackground().setColorFilter(new LightingColorFilter(0x00000000, 0x0000FF00)); // green
button.invalidate();

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

button.getBackground().setColorFilter(new LightingColorFilter(0x00000000, 0x000000FF)); // blue
button.invalidate();
}
}

这只会在 3 秒后显示一个蓝色按钮;它从不显示为绿色。

我认为如果我的大脑正常工作,我可以从以下帖子之一中找出答案:

Why isn't view.invalidate immediately redrawing the screen in my android game

How to force a view to redraw immediately before the next line of code is executed

How to force an entire layout View refresh?

最佳答案

您不应该在 UI 线程上休眠,因为这会挂起您的界面。相反,在最初将颜色设置为绿色后,您可以创建一个处理程序(在 UI 线程上,因此它将使用该线程处理消息),然后使用 Handler.postDelayed发送将在 3 秒内处理的消息。在传递给 postDelayedRunnable 中,您可以将颜色设置为蓝色。

关于android - 在 Android 中,如何更改按钮的颜色,暂停几秒钟,然后再次更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15058490/

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