大家都知道由于性能要求,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的方式,希望对大家的学习有所帮助。
总结以上是内存溢出为你收集整理的Android异步更新UI的四种方式全部内容,希望文章能够帮你解决Android异步更新UI的四种方式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)