动态显示时间,但差异为3秒

时间:2017-03-05 12:31:43

标签: android datetime xamarin time

我看到了这个链接Android Scheduled Timer - Mono for Android or Java

我使用了这段代码,但输出有3秒的差异。我的系统时间是3:56:33,但我的节目输出是3:56:30。

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Diagnostics;
using System.Timers;

namespace App21
{
    [Activity(Label = "App21", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        TextView tv1;
        TextView tv2;
        TextView tv3;
        System.Timers.Timer t;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            tv1 = FindViewById<TextView>(Resource.Id.textView1);
            tv2 = FindViewById<TextView>(Resource.Id.textView2);
            tv3 = FindViewById<TextView>(Resource.Id.textView3);
            t = new System.Timers.Timer();
            t.Elapsed += new ElapsedEventHandler(OnTimeEvent);
            t.Interval = 1000;
            t.Enabled = true;
            t.Start();
        }
        private void OnTimeEvent(object source, ElapsedEventArgs e)
        {
            RunOnUiThread(delegate
            {
                tv1.Text = DateTime.Now.Hour.ToString();
                tv2.Text = DateTime.Now.Minute.ToString();
                tv3.Text = DateTime.Now.Second.ToString();
            });
        }
    }
}

0 个答案:

没有答案