Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在UI线程上运行仅更新最后一个所需结果(Android)_Java_Android_Multithreading_Android Ui - Fatal编程技术网

Java 在UI线程上运行仅更新最后一个所需结果(Android)

Java 在UI线程上运行仅更新最后一个所需结果(Android),java,android,multithreading,android-ui,Java,Android,Multithreading,Android Ui,我正在尝试根据经过的时间更新UI,下面是我的代码: 我的OnCreate方法: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tx = (TextView)findViewById(R.id.textView); startTime =

我正在尝试根据经过的时间更新UI,下面是我的代码:

我的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;
            }

        }
    });
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tx=(TextView)findViewById(R.id.TextView);
startTime=System.nanoTime();
估计时间=(System.nanoTime()-startTime)/100000000;
tx.postInvalidate();
System.out.println(“+”估计时间);
MainActivity.this.runOnUiThread(新的Runnable(){
公开募捐{

虽然(estimatedTime解压/安装速度太快,但这是问题所在


因此,如果您想查看所有日志,只需在文本视图中添加带有+=operator的文本,如果您想一步一步地查看操作,请使用
Thread.Sleep(500)
在每次初始化和设置文本时,在每个if语句中

如果需要将文本附加到文本视图,请使用append而不是setText,在onCreate()内调用runOnUiThread()是没有意义的,因为onCreate()它本身在UI线程上运行。但是如果我不使用它,TextView将不会更新?我如何将代码与onCreate分开?我的意思是如何分支它,很抱歉听起来很困惑,但我无法更好地解释它!你能给我更多关于+=运算符的指导以及如何使用它吗?实际上它的意思是:String tmpString=“;tmpString+=”a”;表示tmpString=tmpString+a;它与int或double或其他形式相同:)