开发者

Problem with Timer and schedule, Android

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:

开发者_StackOverflow中文版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);

 }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜