简体   繁体   English

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

[英]Update Textview text multiple times with few seconds delay

I am trying to update textview text several times but only last text is displayed on screen.我试图多次更新 textview 文本,但屏幕上只显示最后一个文本。 I basically want to delay text appearance so that user gets time to read the message.我基本上想延迟文本外观,以便用户有时间阅读消息。 Below is the code snippet that i have tried.下面是我尝试过的代码片段。 Kindly guide.请指导。 I am using xamarin.我正在使用 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";
            });
        });
    }
}

The above code displayed text "START" and then sleeps for (2+2+2+2 = 8) seconds and then displays only last text (BYE BYE).上面的代码显示文本“START”,然后休眠 (2+2+2+2 = 8) 秒,然后只显示最后一个文本(BYE BYE)。 Kindly guide.请指导。

Use the power of asnyc/await :) It keeps your UI responsive and you don't have to dispatch stuff back to the UI thread manually, because it saves the context.使用 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";
}

Do like this,这样做,

new CountDownTimer(8000, 1000) {

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

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

in setText() methodsetText()方法中

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

You run the update text and the Thread.Sleep on the UI thread, so the UI not receive the change itself because it immediately go to sleep, after that last text update the UI receive control and update itself.您在 UI 线程上运行更新文本和Thread.Sleep ,因此 UI 本身不会收到更改,因为它会立即进入睡眠状态,在最后一个文本更新之后,UI 会接收控制并更新自身。

To view the updating text you need to update your text with Timer (you have several options, all of them very simple to use. just google it) or to do the updating and the waiting ( Thread.Sleep ) in background (via Task for example), and send the update request to UI.要查看更新文本,您需要使用Timer更新文本(您有多个选项,所有选项都非常易于使用。只需 google 即可)或在后台进行更新和等待( Thread.Sleep )(通过Task for示例),并将更新请求发送到 UI。

Just remember that if you updaeting the UI from non UI thread, you need yo do it with Send\\Post and not directly.请记住,如果您从非 UI 线程更新 UI,则需要使用 Send\\Post 而不是直接执行此操作。 (check Dispatcher.Invoke\\BeginInvoke for WPF or other synchronization context for other platforms). (检查 Dispatcher.Invoke\\BeginInvoke 以获得 WPF 或其他平台的其他同步上下文)。

(I don't give specific implementation because you can choose form variant possibilities, if you want any specific example, please comment) (我没有给出具体的实现,因为你可以选择表单变体的可能性,如果你想要任何具体的例子,请评论)

I tried @Sven-Michael Stübe answer as follows:我试过@Sven-Michael Stü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);
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM