• Android 更新UI的几种方式


    1、Activity的 runOnUiThread   

     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()

    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()

    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();
    
        }
    }

    在子线程中切换到主线程

            new Thread(new Runnable() {
                @Override
                public void run() {
                    LogUtil.d( "ttt  11111111111" +  Thread.currentThread().getName() );
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            LogUtil.d( "ttt  55555555"  +  Thread.currentThread().getName() );
                        }
                    });
    
                    LogUtil.d( "ttt  22222222222"  +  Thread.currentThread().getName() );
                    LogUtil.d( "ttt  33333333333"  +  Thread.currentThread().getName() );
                    LogUtil.d( "ttt  44444444444"  +  Thread.currentThread().getName() );
    
                }
            }).start();
    

      结果

     ttt  11111111111Thread-155
     ttt  22222222222Thread-155
     ttt  33333333333Thread-155
     ttt  44444444444Thread-155
     ttt  55555555main
    

      可见这种方式可以快速切换线程,从log日志来看,切换到主线程不会阻塞子线程。

    4、view Post() 

     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();
    

    总结:

    1、其实上面的四种方式都可归结于一种方式:handler 用于Android线程之间的通信。

    2、为什么android要求只能在UI线程进行UI操作? 主要还是为了避免多线程造成的并发的问题。在单线程操作UI是安全的。

  • 相关阅读:
    zlib编译不过(Error A2070)解决方法(转)
    error C2440: 'static_cast' : cannot convert from 'UINT (__thiscall CStaticLink::* )(CPoint)' to 'LRESULT (__thiscall CWnd::* )(CPoint) (转)
    MFC does not support WINVER less than 0x0501 解决方案(转)
    【程序打包工具 Inno Setup】转
    【Visual Studio】简单内存泄漏检测方法 解决 Detected memory leaks! 问题(转)
    各种版本QT下载地址与VS2013+QT5.3.1环境搭建过程(转)
    【Windows Message】MFC 通过F5,刷新桌面
    [bzoj]3343 教主的魔法
    NOIP2012 国王游戏
    NOIP模拟赛 路面修整
  • 原文地址:https://www.cnblogs.com/zhaoyanjun/p/5546683.html
Copyright © 2020-2023  润新知