多次更新Textview文本,延迟几秒钟

时间:2016-03-30 07:42:48

标签: c# android xamarin xamarin.android

我尝试多次更新textview文本,但屏幕上只显示最后一个文本。我基本上想延迟文本外观,以便用户有时间阅读消息。以下是我尝试过的代码段。请指导。我正在使用xamarin。

class SplashScreen : Activity
{

    public SplashScreen() 
    {

    } 

    protected override void OnCreate(Bundle bundle)
    {

        base.OnCreate(bundle);
        RequestWindowFeature (Android.Views.WindowFeatures.NoTitle);
        SetContentView(Resource.Layout.splash_screen);


        MyTextView tv0 = FindViewById<MyTextView> (Resource.Id.BookIntro);
        tv0.Text = "START";

        ThreadPool.QueueUserWorkItem(d =>
        {

        //some code here 


        RunOnUiThread (() => {

            tv0.Text = "HI";
            System.Threading.Thread.Sleep(2000);
            tv0.Text = "HELLO";
            System.Threading.Thread.Sleep(2000);
            tv0.Text = "HOW ARE YOU";
            System.Threading.Thread.Sleep(2000);
            tv0.Text = "WELCOME TO ANDROID";
            System.Threading.Thread.Sleep(2000);
            tv0.Text = "BYE BYE";
            });
        });
    }
}

上面的代码显示文本“START”,然后睡眠(2 + 2 + 2 + 2 = 8)秒,然后只显示最后一个文本(BYE BYE)。请指导。

4 个答案:

答案 0 :(得分:1)

使用asnyc / await的强大功能:)它可以保持您的UI响应,并且您不必手动将内容分发回UI线程,因为它可以保存上下文。

protected override void OnCreate(Bundle bundle)
{

    base.OnCreate(bundle);
    RequestWindowFeature (Android.Views.WindowFeatures.NoTitle);
    SetContentView(Resource.Layout.splash_screen);

    UpdateTextAsync();
}

private async void UpdateTextAsync() 
{
    MyTextView tv0 = FindViewById<MyTextView> (Resource.Id.BookIntro);
    tv0.Text = "HI";
    await Task.Delay(2000);
    tv0.Text = "HELLO";
    await Task.Delay(2000);
    tv0.Text = "HOW ARE YOU";
    await Task.Delay(2000);
    tv0.Text = "WELCOME TO ANDROID";
    await Task.Delay(2000);
    tv0.Text = "BYE BYE";
}

答案 1 :(得分:0)

这样做,

new CountDownTimer(8000, 1000) {

 public void onTick(long millisUntilFinished) {
     setText("step"+millisUntilFinished);
 }

 public void onFinish() {
     //Do something
 }
}.start();
setText()方法

中的

public void setText(String value){
   tv0.Text = value; //Set value to TextView here
}

答案 2 :(得分:0)

您在UI线程上运行更新文本和Thread.Sleep,因此UI本身不会收到更改,因为它会立即进入睡眠状态,在最后一次文本更新后,UI会接收控件并自行更新。

要查看更新文字,您需要使用Timer更新文字(您有多个选项,所有这些选项都非常简单易用。只需google)或进行更新和等待({{1在后台(例如,通过Thread.Sleep),并将更新请求发送到UI。

请记住,如果您从非UI线程更新UI,则需要使用Send \ Post而不是直接执行此操作。 (检查Dispatcher.Invoke \ BeginInvoke for WPF或其他平台的其他同步上下文)。

(我没有给出具体的实施,因为您可以选择表格变体的可能性,如果您想要任何具体的例子,请发表评论)

答案 3 :(得分:0)

我试过@ Sven-MichaelStübe回答如下:

EditText QtyChange = FindViewById<EditText>(Resource.Id.txt_qty_change);
this.QtyChange.TextChanged += QtyChange_TextChanged;

private async void QtyChange_TextChanged(object sender, TextChangedEventArgs e)
            {
                await System.Threading.Tasks.Task.Delay(600);

                int newQuant;
                if (Int32.TryParse(e.Text.ToString(), out newQuant))
                {
                    // Update text here:
                    something.Text = newQuant
                    adapter.NotifyItemChanged(AdapterPosition);
                }
            }
相关问题