【发布时间】:2012-07-30 16:00:45
【问题描述】:
在我的应用程序中,我在TextView 中显示了一个时钟,我想实时更新它。我试着这样运行它:
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);
}
但它不起作用。
【问题讨论】:
-
“不起作用”从不足以说明问题。 stackoverflow.com/questions/how-to-ask
-
什么不起作用?你有什么错误吗?
-
你试过循环clock()调用而不是executeClock()吗?
-
如何使用计时器并每秒更新一次用户界面,或者如果您愿意,可以更频繁地更新您的用户界面......?灵感:stackoverflow.com/a/6702767/1525300.
-
您的
while循环太快,UI 无法更新。使用Handler。