Android攻城狮 Android中更新UI的几种方式

【Android攻城狮 Android中更新UI的几种方式】男儿欲遂平生志,六经勤向窗前读。这篇文章主要讲述Android攻城狮 Android中更新UI的几种方式相关的知识,希望能为你提供帮助。

android中更新UI的几种方式: 1. Activity 的 runOnUiThread() 2. Handler 的 post() 3. Handler 的 sendMessage() 4. View 的 post()

 
 
1 public class FiveActivity extends Activity { 2 3private TextView textView; 4 5private Handler handler = new Handler() { 6public void handleMessage(android.os.Message msg) { 7textView.setText("ok2"); 8}; 9}; 10 11private void handle1() { 12 13handler.post(new Runnable() { 14 15@Override 16public void run() { 17// TODO Auto-generated method stub 18textView.setText("ok1"); 19} 20}); 21 22} 23 24// 最常见的的 25public void handle2() { 26handler.sendEmptyMessage(1); 27} 28 29public void updateUI() { 30runOnUiThread(new Runnable() { 31 32@Override 33public void run() { 34// TODO Auto-generated method stub 35textView.setText("ok3"); 36} 37}); 38} 39 40public void viewUI() { 41textView.post(new Runnable() { 42 43@Override 44public void run() { 45// TODO Auto-generated method stub 46textView.setText("ok4"); 47} 48}); 49} 50 51@Override 52protected void onCreate(Bundle savedInstanceState) { 53// TODO Auto-generated method stub 54super.onCreate(savedInstanceState); 55setContentView(R.layout.five); 56textView = (TextView) findViewById(R.id.textView1); 57new Thread() { 58public void run() { 59try { 60Thread.sleep(2000); 61// handle1() 62// handle2(); 63// updateUI(); 64viewUI(); 65} catch (InterruptedException e) { 66// TODO Auto-generated catch block 67e.printStackTrace(); 68} 69} 70}.start(); 71} 72 }

 

    推荐阅读