你好,这不是我的主要代码,但它的关闭应该表明我的问题。
我需要创建UI延迟(两次) - 一个接一个 我无法单独使用handler.postDelay。
所以我尝试用线程制作它。
我想做的是让t2在t1结束后才开始。
任何人都可以帮助我吗?
final Handler handler1 = new Handler();
final Handler handler2 = new Handler();
synchronized(this)
{
Thread t1= new Thread(new Runnable() {
public void run()
{
handler1.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s = 3000ms
imgB1.setBackgroundColor(Color.RED);
notifyAll();
}
}, 3000);
}
});
Thread t2= new Thread(new Runnable() {
public void run()
{
handler2.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s = 3000ms
imgB1.setBackgroundColor(Color.YELLOW);
}
}, 3000);
}
});
t1.start();
while (t1.isAlive() )
{
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
t2.start();
}
答案 0 :(得分:2)
保持简单:
final Handler handler1 = new Handler(Looper.getMainLooper());
handler1.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s = 3000ms
imgB1.setBackgroundColor(Color.RED);
//post another action to be executed in 3 sec
handler1.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 3s = 3000ms
imgB1.setBackgroundColor(Color.YELLOW);
}
}, 3000);
}
}, 3000);
答案 1 :(得分:1)
您也可以通过这种方式保持简单:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(3000);
runOnUiThread(new Runnable() {
@Override
public void run() {
imgB1.setBackgroundColor(Color.RED);
}
});
SystemClock.sleep(3000);
runOnUiThread(new Runnable() {
@Override
public void run() {
imgB1.setBackgroundColor(Color.YELLOW);
}
});
}
});
t.start();