简体   繁体   English

在UI线程上运行仅更新最后想要的结果(Android)

[英]Run on UI thread updates only the last desired result (Android)

I am trying to do some task of updating the UI on the basis of elapsed time, following is my code: 我正在尝试根据经过的时间来更新UI,这是我的代码:

My OnCreate method: 我的OnCreate方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tx = (TextView)findViewById(R.id.textView);

    startTime = System.nanoTime();
    estimatedTime = (System.nanoTime() - startTime) / 1000000000;
    tx.postInvalidate();
    System.out.println(""+estimatedTime);
      MainActivity.this.runOnUiThread(new Runnable(){
        public void run() {
            while (estimatedTime <= 100){
                System.out.println(""+estimatedTime);

                if(estimatedTime == 10){
                    TextView tx = (TextView)findViewById(R.id.textView);
                    tx.setText("Shri Ram Raksha Stotram"); 
                    System.out.println("Yay");
                }
                if(estimatedTime == 20){
                    TextView tx = (TextView)findViewById(R.id.textView);
                    tx.setText("Inatializing"); 
                    System.out.println("Yay");
                }
                if(estimatedTime == 30){
                    TextView tx = (TextView)findViewById(R.id.textView);
                    tx.setText("Preparing to install"); 
                    System.out.println("Yay");
                }
                if(estimatedTime == 40){
                    TextView tx = (TextView)findViewById(R.id.textView);
                    tx.setText("Installing"); 
                    System.out.println("Yay");
                }
                if(estimatedTime == 50){


                    TextView tx = (TextView)findViewById(R.id.textView);
                    tx.setText("Installed"); 
                    System.out.println("Yay");
                }
                if(estimatedTime == 60){
                    TextView tx = (TextView)findViewById(R.id.textView);
                    tx.setText("Unzipping packages..."); 
                    System.out.println("Yay");
                }


                estimatedTime = (System.nanoTime() - startTime) / 1000000000;
            }

        }
    });
}

All of the above code is on the OnCreate method, however even after implementing a RunonUI thread I am not able to update the textview, the only result I get on the textview is the last if condition "Unzipping Packages". 上面的所有代码都在OnCreate方法上,但是即使实现了RunonUI线程后,我也无法更新textview,但在textview上获得的唯一结果是最后一个条件“解压缩包”。 Where am I going wrong? 我要去哪里错了?

It's unzipping/installing too fast, it's the problem. 解压缩/安装速度太快了,这就是问题所在。

So if you want to see all log just append text with += operator to your textview if you want to see operations step by step then use Thread.Sleep(500); 因此,如果您想查看所有日志,只需要将带有+ =运算符的文本追加到textview中,如果您想逐步查看操作,请使用Thread.Sleep(500); in your every if statements 在您的每个if语句中

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

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