简体   繁体   English

倒数计时器至特定日期

[英]Countdown timer to specific date

I'm trying to make a countdown timer that displays total number of days, hours, minutes and seconds remaining to specific date. 我正在尝试制作一个倒数计时器,以显示特定日期剩余的天数,小时数,分钟数和秒数。

That's what I've up to now created. 这就是我到目前为止所创建的。

protected override void OnCreate (Bundle bundle)
{
  base.OnCreate (bundle);


  SetContentView (Resource.Layout.Main);

  txtDays = FindViewById<TextView> (Resource.Id.txtDays);
  txtHours = FindViewById<TextView> (Resource.Id.txtHours);
  txtMins = FindViewById<TextView> (Resource.Id.txtMins);
  txtSec = FindViewById<TextView> (Resource.Id.txtSec);

  DateTime enteredDate = new DateTime(2013, 7, 25, 12 ,30 ,00);

  DateTime todaysDateTime = DateTime.Now;
  DateTime formattedDate = todaysDateTime.AddHours (2);

  TimeSpan span = enteredDate.Subtract(formattedDate);

  double totalDays = span.TotalDays;
  double totalHours = span.TotalHours;
  double totalMins = span.TotalMinutes;
  double totalSec = span.TotalSeconds;

  new Thread(new ThreadStart(() =>
                             {
    RunOnUiThread(() =>
                  {
      Console.WriteLine ("Days: " + String.Format("{0:0}", Math.Truncate(totalDays)));
      Console.WriteLine ("Hours: " + String.Format("{0:0}", Math.Truncate(totalHours)));
      Console.WriteLine ("Minutes: " + String.Format("{0:0}", Math.Truncate(totalMins)));
      Console.WriteLine ("Seconds: " + String.Format("{0:0}", Math.Truncate(totalSec)));

      txtDays.Text = String.Format ("{0:0}", Math.Truncate (totalDays));
      txtHours.Text = String.Format ("{0:0}", Math.Truncate (totalHours));
      txtMins.Text = String.Format ("{0:0}", Math.Truncate (totalMins));
      txtSec.Text = String.Format ("{0:0}", Math.Truncate (totalSec));

  });
  })).Start();  

}

How to update TextViews every second automatically with Android C#? 如何使用Android C#自动每秒更新一次TextViews?

EDIT 2: 编辑2:

I have used Timer, it's counting with Console.WriteLine, but the TextViews displays nothing and they don't updating... Somebody an idea how to update TextViews every second? 我用过Timer,它通过Console.WriteLine进行计数,但是TextViews不显示任何内容,并且它们不更新...有人知道如何每秒更新一次TextViews吗?

timer = 0;
      new Thread(new ThreadStart(() =>
                                 {
      Thread.Sleep (1000);
        RunOnUiThread(() =>
                      {
      tmr.Elapsed += new System.Timers.ElapsedEventHandler(tmr_Elapsed);
      tmr.Start();
      while (timer < totalSec) ;
      tmr.Stop();

        });
      })).Start(); 

void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
      txtDays = FindViewById<TextView> (Resource.Id.txtDays);
      txtHours = FindViewById<TextView> (Resource.Id.txtHours);
      txtMins = FindViewById<TextView> (Resource.Id.txtMins);
      txtSec = FindViewById<TextView> (Resource.Id.txtSec);

      DateTime enteredDate = new DateTime(2013, 7, 25, 12 ,30 ,00);

      DateTime todaysDateTime = DateTime.Now;
      DateTime formattedDate = todaysDateTime.AddHours (2);

      TimeSpan span = enteredDate.Subtract(formattedDate);

      totalDays = span.TotalDays;
      totalHours = span.TotalHours;
      totalMins = span.TotalMinutes;
      totalSec = span.TotalSeconds;

      Console.WriteLine ("Days: " + String.Format("{0:0}", Math.Truncate(totalDays)));
      Console.WriteLine ("Hours: " + String.Format("{0:0}", Math.Truncate(totalHours)));
      Console.WriteLine ("Minutes: " + String.Format("{0:0}", Math.Truncate(totalMins)));
      Console.WriteLine ("Seconds: " + String.Format("{0:0}", Math.Truncate(totalSec)));

      txtDays.Text = String.Format ("{0:0}", Math.Truncate (totalDays));
      txtHours.Text = String.Format ("{0:0}", Math.Truncate (totalHours));
      txtMins.Text = String.Format ("{0:0}", Math.Truncate (totalMins));
      txtSec.Text = String.Format ("{0:0}", Math.Truncate (totalSec));

    }

在C#中,使用Timer类并将其设置为1秒。

Problem solved just added RunOnUiThread within tmr_Elapsed 问题解决了,只需在tmr_Elapsed中添加RunOnUiThread

void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
  RunOnUiThread(() =>
                {
  txtDays = FindViewById<TextView> (Resource.Id.txtDays);
  txtHours = FindViewById<TextView> (Resource.Id.txtHours);
  txtMins = FindViewById<TextView> (Resource.Id.txtMins);
  txtSec = FindViewById<TextView> (Resource.Id.txtSec);

  DateTime enteredDate = new DateTime(2013, 7, 25, 12 ,30 ,00);

  DateTime todaysDateTime = DateTime.Now;
  DateTime formattedDate = todaysDateTime.AddHours (2);

  TimeSpan span = enteredDate.Subtract(formattedDate);

  totalDays = span.TotalDays;
  totalHours = span.TotalHours;
  totalMins = span.TotalMinutes;
  totalSec = span.TotalSeconds;

  Console.WriteLine ("Days: " + String.Format("{0:0}", Math.Truncate(totalDays)));
  Console.WriteLine ("Hours: " + String.Format("{0:0}", Math.Truncate(totalHours)));
  Console.WriteLine ("Minutes: " + String.Format("{0:0}", Math.Truncate(totalMins)));
  Console.WriteLine ("Seconds: " + String.Format("{0:0}", Math.Truncate(totalSec)));


  txtHours.Text = String.Format ("{0:0}", Math.Truncate (totalHours));
  txtHours.Text = String.Format ("{0:0}", Math.Truncate (totalHours));
  txtMins.Text = String.Format ("{0:0}", Math.Truncate (totalMins));
  txtSec.Text = String.Format ("{0:0}", Math.Truncate (totalSec));
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM