计时器和日程安排问题,Android

发布于 2024-09-18 13:21:54 字数 619 浏览 3 评论 0原文

我刚刚学习和锻炼@ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

著墨染雨君画夕 2024-09-25 13:21:54

使用处理程序将时间更改传递给文本视图。请参阅:

如何更改文本在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文