与onClickListener奇怪的线程的行为 [英] Strange thread behavior with onClickListener
问题描述
我想改变一个按钮,1秒钟pssed $ P $'的背景,然后更改背景回默认。我创建了一个线程调用卧铺,仅仅睡了1秒。下面是我使用的方法。奇怪的是,按钮背景不会改变,直到卧铺线程完成睡觉。我知道线程保证为从未在任何给定的时间启动,但由于我设置按钮的背景开始线程之前我想这应该工作。
I am trying to change the background of a button to 'pressed' for 1 second, then change that background back to 'default'. I created one Thread called sleeper that simply sleeps for 1 second. Below is the method I am using. The strange thing is, the buttons background doesn't change until the sleeper thread is done sleeping. I know threads are never guarenteed to start at any given time but since I am setting the background of the button before starting the Thread I figured this should work.
private void handleButtonPress(View button, int buttonPressedID,
int buttonNormalID) {
button.setBackgroundResource(buttonPressedID);
Sleeper sleeper = new Sleeper();
sleeper.start();
try {
sleeper.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
感谢在看看!
推荐答案
请简单地使用该处理程序:
Please simply use a Handler for that:
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
// do your delayed stuff here
}
}, 1000); // execute the code inside run() after 1000ms
这篇关于与onClickListener奇怪的线程的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!