0

嘿,伙计们,这是我在 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 秒后将“HI”添加到我的 TextView,然后在另一个计时器停止时在另一个 5 秒后添加“BYE”,然后最后但并非最不重要的是在“后 5 秒”添加“TRI” BYE”,然后重新开始整个事情。但这对我不起作用,我只得到一个“HI”,然后它停止做任何事情。

我会很感激任何帮助,因为这是我需要用我的应用程序完成的最后一件事:)

顺便说一句,我对 C# 很陌生...

4

1 回答 1

2

您的计时器很好,但是它们在自己的线程上运行,因此您需要RunOnUiThread在想要更新 UI 元素时使用。

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;
    }
}
于 2013-02-26T23:33:49.843 回答