Android中在子线程中更新UI

时间:2021-03-10 05:10:27

1、Activity的 runOnUiThread  

1
2
3
4
5
6
7
8
9
10
11
12
13
textView = (TextView) findViewById( R.id.tv );
        new  Thread( new  Runnable() {
            @Override
            public  void  run() {
 
                runOnUiThread( new  Runnable() {
                    @Override
                    public  void  run() {
                        textView.setText(  "更新UI了" );
                    }
                });
            }
        }).start();

android Activity runOnUiThread() 方法使用

2、Handler sendEmptyMessage()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package  lib.com.myapplication;
 
import  android.os.Handler;
import  android.os.Message;
import  android.support.v7.app.AppCompatActivity;
import  android.os.Bundle;
import  android.widget.TextView;
 
public  class  MainActivity  extends  AppCompatActivity {
 
     private  TextView textView ;
 
     Handler handler =  new  Handler( ) {
         @Override
         public  void  handleMessage(Message msg) {
             super .handleMessage(msg);
             textView.setText(  "Ui更新了" );
         }
     };
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         textView = (TextView) findViewById( R.id.tv );
         new  Thread( new  Runnable() {
             @Override
             public  void  run() {
                 try  {
                     Thread.sleep(  2000  );
                 catch  (InterruptedException e) {
                     e.printStackTrace();
                 }
 
                 handler.sendEmptyMessage(  2  ) ;
             }
         }).start();
 
     }
}

3、Handler  post()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package  lib.com.myapplication;
import  android.os.Bundle;
import  android.os.Handler;
import  android.support.v7.app.AppCompatActivity;
import  android.widget.TextView;
 
public  class  MainActivity  extends  AppCompatActivity {
 
     private  TextView textView ;
 
     Handler handler =  new  Handler();
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         textView = (TextView) findViewById( R.id.tv );
 
         new  Thread( new  Runnable() {
             @Override
             public  void  run() {
                 try  {
                     Thread.sleep(  2000  );
                 catch  (InterruptedException e) {
                     e.printStackTrace();
                 }
 
                 handler.post( new  Runnable() {
                     @Override
                     public  void  run() {
                         textView.setText(  "Ui更新了" );
                     }
                 }) ;
             }
         }).start();
 
     }
}

4、view Post() 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
textView = (TextView) findViewById( R.id.tv );
        new  Thread( new  Runnable() {
            @Override
            public  void  run() {
                try  {
                    Thread.sleep(  2000  );
                catch  (InterruptedException e) {
                    e.printStackTrace();
                }
 
                textView.post( new  Runnable() {
                    @Override
                    public  void  run() {
                        textView.setText(  "Ui更新了" );
                    }
                }) ;
            }
        }).start();