Aandroid 总结4种线程中操作UI界面的方法

总结4种线程中操作UI界面的方法
 
我们经常会在后台线程中去做一些耗时的操作,比如去网络取数据。但是当数据取回来,需要显示到页面上的时候,会遇到一些小麻烦,因为我们都知道,android的UI页面是不允许在其他线程直接操作的。下面总结4中方法用来在线程中操作UI界面。
 
  1 package com.chensw.uvinthread;
  2  
  3 import android.annotation.SuppressLint;
  4 import android.app.Activity;
  5 import android.app.ProgressDialog;
  6 import android.os.AsyncTask;
  7 import android.os.Bundle;
  8 import android.os.ConditionVariable;
  9 import android.os.Handler;
 10 import android.os.Message;
 11 import android.view.Menu;
 12 import android.view.View;
 13 import android.widget.Button;
 14 import android.widget.TextView;
 15  
 16 public class MainActivity extends Activity {
 17 private TextView textView = null;
 18 private TextView textView1 = null;
 19 private TextView textView2 = null;
 20 private TextView textView3 = null;
 21 private Handler handler = null;
 22  
 23 private Handler showHandler = null;
 24 private Button myBtn = null;
 25 private ProgressDialog progressDialog = null;
 26 private static final int DISMISS_PROGRESS_DIALOG = 1;
 27 private Message message = null;
 28 private ConditionVariable mCondition;
 29  
 30 @Override
 31 protected void onCreate(Bundle savedInstanceState) {
 32 super.onCreate(savedInstanceState);
 33 setContentView(R.layout.activity_main);
 34  
 35 textView = (TextView) findViewById(R.id.textView);
 36 textView1 = (TextView) findViewById(R.id.textView1);
 37 textView2 = (TextView) findViewById(R.id.textView2);
 38 textView3 = (TextView) findViewById(R.id.textView3);
 39 myBtn = (Button) findViewById(R.id.button1);
 40  
 41 // 方法一,使用handler进行修改
 42 // 实例化内部类Handler
 43 handler = new MyHandler();
 44 // 启动新线程,在线程中调用handler的方法
 45 new Thread() {
 46 public void run() {
 47 // 可以使用Message类实例进行参数传递
 48 handler.handleMessage(null);
 49 }
 50 }.start();
 51  
 52 // 方法二,使用View.post(Runnable)方法修改-----注意:方法二不能与方法三,方法四并存在同一UI线程中
 53 new Thread(new ViewRunnable()).start();
 54  
 55 // 方法三,使用Activity.runOnUiThread(Runnable);
 56 new Thread(new UIThead()).start();
 57  
 58 // 方法四,使用AsyncTask
 59 new UIAsyncTask().execute(null, null);
 60  
 61 // 一个显示ProgressDialog的简单例子
 62 showHandler = new ShowHandler();
 63 mCondition = new ConditionVariable();
 64 myBtn.setOnClickListener(new ShowButtonListener());
 65  
 66 }
 67  
 68 // 编写按钮监听类
 69 private class ShowButtonListener implements View.OnClickListener {
 70 @Override
 71 public void onClick(View arg0) {
 72 progressDialog = ProgressDialog.show(MainActivity.this, "请稍等...",
 73 "加载中...", true);
 74 new Thread() {
 75 @Override
 76 public void run() {
 77 // 线程阻塞的两种方法
 78 try {
 79 Thread.sleep(2000);
 80 } catch (InterruptedException e) {
 81 e.printStackTrace();
 82 }
 83  
 84 //mCondition.block(5000);
 85  
 86 message = showHandler
 87 .obtainMessage(DISMISS_PROGRESS_DIALOG);
 88 showHandler.sendMessage(message);
 89 }
 90 }.start();
 91 }
 92 }
 93  
 94 /*
 95 * 在Handler中停止ProgressDialog
 96 */
 97 @SuppressLint("HandlerLeak")
 98 private class ShowHandler extends Handler {
 99 @Override
100 public void handleMessage(Message msg) {
101 switch (msg.what) {
102 case DISMISS_PROGRESS_DIALOG:
103 progressDialog.dismiss();
104 break;
105 default:
106 break;
107 }
108 }
109 };
110  
111 // 方法四,使用AsyncTask
112 private class UIAsyncTask extends AsyncTask<Void, Void, String> {
113 @Override
114 protected String doInBackground(Void... params) {
115 return "UIAsyncTask";
116 }
117  
118 @Override
119 protected void onPostExecute(String result) {
120 textView3.setText(result);
121 }
122  
123 }
124  
125 /*
126 * 方法三,使用Activity.runOnUiThread(Runnable);
127 */
128 private class UIThead implements Runnable {
129 @Override
130 public void run() {
131 runOnUiThread(new Runnable() {
132 public void run() {
133 textView2.setText("UIThead");
134 }
135 });
136 }
137 }
138  
139 /*
140 * 方法二,使用View.post(Runnable)方法修改; 编写内部类实现Runnable接口,在内部类中修改UI
141 */
142 private class ViewRunnable implements Runnable {
143 @Override
144 public void run() {
145 // 在线程中调用子线程修改UI,子线程的调用使用View.post(Runnable)
146 textView1.post(new Runnable() {
147 @Override
148 public void run() {
149 textView1.setText("MyRunnable");
150 }
151 });
152 }
153 }
154  
155 /*
156 * 方法一,使用handler进行修改; 编写内部类继承Handler类,在内部类中修改UI
157 */
158  
159 @SuppressLint("HandlerLeak")
160 private class MyHandler extends Handler {
161 @Override
162 public void handleMessage(Message msg) {
163 textView.setText("MyHandler");
164 }
165 };
166  
167 @Override
168 public boolean onCreateOptionsMenu(Menu menu) {
169 // Inflate the menu; this adds items to the action bar if it is present.
170 getMenuInflater().inflate(R.menu.main, menu);
171 return true;
172 }
173  
174 }
175  
原文地址:https://www.cnblogs.com/xiaxiaoping/p/3049576.html