运行code一遍遍 [英] Run code over and over
本文介绍了运行code一遍遍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的申请,我显示一个时钟的TextView
,我想实时更新。我试着像这样运行:
In my application I am showing a clock in a TextView
, and I want to update it in real time. I tried to run it like this:
public void clock() {
while(clock_on == true) {
executeClock();
}
}
public void executeClock() {
TextView timeTv = (TextView) findViewById(R.id.time);
long currentTime=System.currentTimeMillis();
Calendar cal=Calendar.getInstance();
cal.setTimeInMillis(currentTime);
String showTime=String.format("%1$tI:%1$tM %1$Tp",cal);
timeTv.setText(showTime);
}
不过,这是行不通的。
But it doesn't work.
推荐答案
请尝试:
private Handler handler = new Handler();
runnable.run();
private Runnable runnable = new Runnable()
{
public void run()
{
//
// Do the stuff
//
if(clock_on == true) {
executeClock();
}
handler.postDelayed(this, 1000);
}
};
这篇关于运行code一遍遍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文