嘿伙计这是我在stackoverflow上的第一个问题:) 所以要解决我的问题: 我有3种不同的Timer类,我想在不同的时间调用它们:
public class Activity1 : Activity
{
static public Timer Kundenerz = new Timer();
static public Timer Kundenerz2 = new Timer();
static public Timer Kundenerz3 = new Timer();
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
Kundenerz.Interval = 5000;
Kundenerz.Elapsed += new ElapsedEventHandler (Kundengroup);
Kundenerz.Enabled = true;
Kundenerz2.Interval = 5000;
Kundenerz2.Elapsed += new ElapsedEventHandler (Kundengroup2);
Kundenerz3.Interval = 5000;
Kundenerz3.Elapsed += new ElapsedEventHandler (Kundengroup3);
{
public void Kundengroup(object sender, ElapsedEventArgs e)
{
var textView1 = FindViewById<TextView> (Resource.Id.textView1);
Kundenerz.Enabled = false;
textView1.Append ("HI");
textView1.Append ("\r\n");
Kundenerz2.Interval = 5000;
Kundenerz2.Enabled = true;
}
public void Kundengroup2(object sender, ElapsedEventArgs e)
{
var textView1 = FindViewById<TextView> (Resource.Id.textView1);
Kundenerz2.Enabled = false;
textView1.Append ("BYE");
textView1.Append ("\r\n");
Kundenerz3.enabled = true;
}
public void Kundengroup3(object sender, ElapsedEventArgs e)
{
var textView1 = FindViewById<TextView> (Resource.Id.textView1);
Kundenerz3.Enabled = false;
textView1.Append ("TRI");
textView1.Append ("\r\n");
Kundenerz.Enabled = true;
}
}
我希望我的程序的这一部分在前5秒后在我的TextView中添加“HI”,然后在另一个计时器停止后在另外5秒后添加“BYE”,然后最后但并非最不重要的是向它添加“TRI”5在“BYE”之后的几秒钟,然后重新开始整个事情。 这对我来说不起作用,我只得到一个“HI”,然后停止做任何事情。
我会感激任何帮助,因为这是我需要用我的应用程序完成的最后一件事:)
我对C#btw ...
很新答案 0 :(得分:2)
您的计时器很好,但是他们在自己的线程上运行,因此在想要更新UI元素时需要使用RunOnUiThread
。
public class Activity1 : Activity
{
public static Timer Kundenerz = new Timer();
public static Timer Kundenerz2 = new Timer();
public static Timer Kundenerz3 = new Timer();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Kundenerz.Interval = 5000;
Kundenerz.Elapsed += Kundengroup;
Kundenerz.Enabled = true;
Kundenerz2.Interval = 5000;
Kundenerz2.Elapsed += Kundengroup2;
Kundenerz3.Interval = 5000;
Kundenerz3.Elapsed += Kundengroup3;
}
public void Kundengroup(object sender, ElapsedEventArgs e)
{
var textView1 = FindViewById<TextView>(Resource.Id.textView1);
Kundenerz.Enabled = false;
RunOnUiThread(() =>
{
textView1.Append("HI");
textView1.Append("\r\n");
});
Kundenerz2.Interval = 5000;
Kundenerz2.Enabled = true;
}
public void Kundengroup2(object sender, ElapsedEventArgs e)
{
var textView1 = FindViewById<TextView>(Resource.Id.textView1);
Kundenerz2.Enabled = false;
RunOnUiThread(() =>
{
textView1.Append("BYE");
textView1.Append("\r\n");
});
Kundenerz3.Enabled = true;
}
public void Kundengroup3(object sender, ElapsedEventArgs e)
{
var textView1 = FindViewById<TextView>(Resource.Id.textView1);
Kundenerz3.Enabled = false;
RunOnUiThread(() =>
{
textView1.Append("TRI");
textView1.Append("\r\n");
});
Kundenerz.Enabled = true;
}
}