android 更新ui的几种方式,详解android进行异步更新UI的四种方式

大家都知道由于性能要求,Android要求只能在UI线程中更新UI,要想在其他线程中更新UI,我大致总结了4种方式,欢迎补充纠正:

使用Handler消息传递机制;

使用AsyncTask异步任务;

使用runOnUiThread(action)方法;

使用Handler的post(Runnabel r)方法;

下面分别使用四种方式来更新一个TextView。

1.使用Handler消息传递机制

package com.example.runonuithreadtest;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.widget.TextView;

public class MainActivity extends Activity {

private TextView tv;

Handler handler = new Handler()

{

public void handleMessage(android.os.Message msg) {

if(msg.what==0x123)

{

tv.setText("更新后的TextView");

}

};

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

new MyThread().start();

}

class MyThread extends Thread

{

@Override

public void run() {

//延迟两秒更新

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

handler.sendEmptyMessage(0x123);

}

}

}

2. 使用AsyncTask异步任务

注:更新UI的操作只能在onPostExecute(String result)方法中。

package com.example.runonuithreadtest;

import android.app.Activity;

import android.os.AsyncTask;

import android.os.Bundle;

import android.widget.TextView;

public class MainActivity extends Activity {

private TextView tv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

new Yibu().execute();

}

class Yibu extends AsyncTask

{

@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.setText("更新后的TextView");

}

}

}

3. 使用runOnUiThread(action)方法

package com.example.runonuithreadtest;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

public class MainActivity extends Activity {

private TextView tv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

new MyThread().start();

}

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.setText("更新后的TextView");

}

});

}

}

}

4. 使用Handler的post(Runnabel r)方法

package com.example.runonuithreadtest;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.widget.TextView;

public class MainActivity extends Activity {

private TextView tv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

Handler handler = new Handler();

handler.post(new Runnable(){

@Override

public void run() {

try {

//延迟两秒更新

Thread.sleep(2000);

} catch (InterruptedException e) {

e.printStackTrace();

}

tv.setText("更新后的TextView");

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

时间: 2017-05-22

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值