Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android異步更新UI的四種方式

Android異步更新UI的四種方式

編輯:關於Android編程

大家都知道由於性能要求,android要求只能在UI線程中更新UI,要想在其他線程中更新UI,大致有4種方式,下面分別使用四種方式來更新一個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<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.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");
  }
 });
 }
}

以上就是四種Android異步更新UI的方式,希望對大家的學習有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved