计时器和日程安排问题,Android
我刚刚学习和锻炼@ Android,我尝试让 TextView 显示当前时间,这意味着 TextView 每秒都会更新。现在我知道可能有很多更好的方法 - 日历、数字时钟和其他小部件可以正确地做到这一点,但正如我所说,我只是在培训。
我的问题是我的“时间表”不起作用......或者我相信。我有一个简单的 XML 布局。这是我的Java代码:
public void onClick(View v)
{
View parent = (View) v.getParent();
final Time tm = new Time();
final TextView tv = (TextView) parent.findViewById(R.id.tv);
Timer timer = new Timer();
TimerTask tt = new TimerTask() {
@Override
public void run() {
tm.setToNow();
tv.setText(time.hour + ":" + time.minute + ":" + time.second);
}
};
timer.scheduleAtFixedRate(tt, 0, 1000);
}
I am just learning and exercising @ Android, and I tried to make a TextView display the current time, meaning the TextView gets updated every second. Now I know there are probably loads of better ways - Calendar's, DigitalClocks and other widgets to do this properly, but as I said, I am merely training.
My problem is that my "schedule" is not working... or I so I believe. I have a simple XML layout. Here is my Java code:
public void onClick(View v)
{
View parent = (View) v.getParent();
final Time tm = new Time();
final TextView tv = (TextView) parent.findViewById(R.id.tv);
Timer timer = new Timer();
TimerTask tt = new TimerTask() {
@Override
public void run() {
tm.setToNow();
tv.setText(time.hour + ":" + time.minute + ":" + time.second);
}
};
timer.scheduleAtFixedRate(tt, 0, 1000);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用处理程序将时间更改传递给文本视图。请参阅:
如何更改文本在 Android TextView 中,
你的想法是正确的,只需将计时器连接到处理程序
use handler to pass the time change to the textview..see:
how to change text in Android TextView
You have the right idea just need to wire timer to handler