码迷,mamicode.com
首页 > 移动开发 > 详细

Android 更新UI的几种方式

时间:2016-06-12 20:12:52      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

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

    }
}

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是安全的。

 

Android 更新UI的几种方式

标签:

原文地址:http://www.cnblogs.com/zhaoyanjun/p/5546683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!