Android 更新UI

Posted 可爱的黑精灵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 更新UI相关的知识,希望对你有一定的参考价值。

本篇简单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的主要内容,如果未能解决你的问题,请参考以下文章

Android:BottomNavigationView第一个片段开关超级延迟

Android非UI片段使用[重复]

java [Fragment片段] #Android #UI

如何在使用片段和计时器的选项卡式活动上更新 UI

Android - 将搜索传递给片段

iOS 相当于 Android 片段/布局