C# 特定日期的倒计时计时器
我正在尝试制作一个倒计时计时器,显示到特定日期剩余的总天数、小时数、分钟数和秒数 这就是我到目前为止所创造的C# 特定日期的倒计时计时器,c#,android,timer,countdown,C#,Android,Timer,Countdown,我正在尝试制作一个倒计时计时器,显示到特定日期剩余的总天数、小时数、分钟数和秒数 这就是我到目前为止所创造的 protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Main); txtDays = FindViewById<TextView> (Resource.Id.txtDays); txtHours
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();
}
protectedoverride void OnCreate(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
txtDays=findviewbyd(Resource.Id.txtDays);
txtHours=findviewbyd(Resource.Id.txtHours);
txtMins=findviewbyd(Resource.Id.txtMins);
txtSec=findviewbyd(Resource.Id.txtSec);
DateTime enteredDate=新的日期时间(2013,7,25,12,30,00);
DateTime todaysDateTime=DateTime.Now;
DateTime formattedDate=todaysDateTime.AddHours(2);
TimeSpan=输入日期减去(格式化日期);
双倍总天数=span.totalDays;
双倍总小时数=span.totalHours;
double totalMins=span.totalMins;
double totalSec=span.TotalSeconds;
新线程(新线程开始(()=>
{
RunOnUiThread(()=>
{
Console.WriteLine(“Days:+String.Format({0:0}),Math.Truncate(totalDays));
Console.WriteLine(“Hours:+String.Format({0:0}),Math.Truncate(totalHours));
Console.WriteLine(“分钟:”+String.Format({0:0}),Math.Truncate(totalMins));
Console.WriteLine(“秒:”+String.Format({0:0}),Math.Truncate(totalSec));
Text=String.Format(“{0:0}”,Math.Truncate(totalDays));
Text=String.Format(“{0:0}”,Math.Truncate(totalHours));
Text=String.Format(“{0:0}”,Math.Truncate(totalMins));
Text=String.Format(“{0:0}”,Math.Truncate(totalSec));
});
})).Start();
}
如何使用Android C每秒钟自动更新文本视图
编辑2:
我用过定时器,它用Console.WriteLine计数,但是TextView什么也不显示,也不更新。。。有人知道如何每秒更新文本视图吗
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;
新线程(新线程开始(()=>
{
睡眠(1000);
RunOnUiThread(()=>
{
tmr.appeased+=新系统计时器ElapsedEventHandler(tmr_appeased);
tmr.Start();
同时(定时器<总秒);
tmr.Stop();
});
})).Start();
无效tmr_已过(对象发送器,System.Timers.ElapsedEventArgs e)
{
txtDays=findviewbyd(Resource.Id.txtDays);
txtHours=findviewbyd(Resource.Id.txtHours);
txtMins=findviewbyd(Resource.Id.txtMins);
txtSec=findviewbyd(Resource.Id.txtSec);
DateTime enteredDate=新的日期时间(2013,7,25,12,30,00);
DateTime todaysDateTime=DateTime.Now;
DateTime formattedDate=todaysDateTime.AddHours(2);
TimeSpan=输入日期减去(格式化日期);
totalDays=span.totalDays;
totalHours=span.totalHours;
totalMins=span.totalMins;
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(“分钟:”+String.Format({0:0}),Math.Truncate(totalMins));
Console.WriteLine(“秒:”+String.Format({0:0}),Math.Truncate(totalSec));
Text=String.Format(“{0:0}”,Math.Truncate(totalDays));
Text=String.Format(“{0:0}”,Math.Truncate(totalHours));
Text=String.Format(“{0:0}”,Math.Truncate(totalMins));
Text=String.Format(“{0:0}”,Math.Truncate(totalSec));
}
在C中#使用定时器
类并将其设置为1秒。问题已解决,只需在tmr中添加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));
});
}
void tmr\u已过(对象发送器,System.Timers.ElapsedEventArgs e)
{
RunOnUiThread(()=>
{
txtDays=findviewbyd(Resource.Id.txtDays);
txtHours=findviewbyd(Resource.Id.txtHours);
txtMins=findviewbyd(Resource.Id.txtMins);
txtSec=findviewbyd(Resource.Id.txtSec);
DateTime enteredDate=新的日期时间(2013,7,25,12,30,00);
DateTime todaysDateTime=DateTime.Now;
DateTime formattedDate=todaysDateTime.AddHours(2);
TimeSpan=输入日期减去(格式化日期);
totalDays=span.totalDays;
totalHours=span.totalHours;
totalMins=span.totalMins;
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(“分钟:”+String.Format({0:0}),Math.Truncate(totalMins));
Console.WriteLine(“秒:”+String.Format({0:0}),Math.Truncate(totalSec));
Text=String.Format(“{0:0}”,Math.Truncate(totalHours));
Text=String.Format(“{0:0}”,Math.Truncate(totalHours));
Text=String.Format(“{0:0}”,Math.Truncate(totalMins));
Text=String.Format(“{0:0}”,Math.Truncate(totalSec));
});
}