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

Android 更新UI

时间:2018-09-17 15:21:56      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:ide   get   xtend   分享   extend   Stub   creat   turn   stat   

本篇简单Android里面更新UI的四种方法。

更新UI简介

Android 更新UI主要是主线程进行更新,即UI线程更新。如果在主线程外更新则会报错。

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

  • 只有创建这个视图层次的线程才能修改它的视图

Handler消息传递


package com.example.chenjy.chenjy;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;


public class MainActivity extends Activity {

    private TextView tv_token;

    Handler handler = new Handler()
    {
        public void handleMessage(Message msg) {
            if(msg.what==0x12)
            {
                tv_token.setText("update UI");
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        new MyThread().start();
    }

    public void initView(){

        tv_token = (TextView)findViewById(R.id.tv_token);

    }

    class MyThread extends Thread
    {
        @Override
        public void run() {
            //延迟两秒更新
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            handler.sendEmptyMessage(0x12);
        }
    }
}

使用runOnUiThread


package com.example.chenjy.chenjy;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView tv_token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        new MyThread().start();
    }

    public void initView(){

        tv_token = (TextView)findViewById(R.id.tv_token);

    }

    class MyThread extends Thread
    {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        //延迟两秒更新
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    tv_token.setText("update UI");
                }
            });
        }
    }
}

Handlerpost(Runnable runnable)


package com.example.chenjy.chenjy;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView tv_token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_token = (TextView) findViewById(R.id.tv_token);

        Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //延迟两秒更新
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                tv_token.setText("update UI");
            }

        });

    }

}

AsyncTask异步任务


package com.example.chenjy.chenjy;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView tv_token;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv_token = (TextView) findViewById(R.id.tv_token);

        new UpdateUI().execute();
    }

    class UpdateUI extends AsyncTask<String, String, String>
    {

        @Override
        protected String doInBackground(String... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            tv_token.setText("update UI");
        }

    }
}

技术分享图片

Android 更新UI

标签:ide   get   xtend   分享   extend   Stub   creat   turn   stat   

原文地址:https://www.cnblogs.com/chenjy1225/p/9662138.html

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