2013-06-19 111 views
0

我正在尝试使倒数计时器显示剩余的特定日期的总天数,小时数,分钟数和秒数。倒数计时器到特定日期

这就是我现在创建的。

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(); 

} 

如何使用Android C#自动每秒更新TextViews?

编辑2:

我已经使用定时器,它的计数为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)); 

    } 

回答

0

问题解决了刚添加RunOnUiThread内tmr_Elapsed

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)); 
    }); 
} 
1

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