Android异步更新UI的四种方式
程序员文章站
2024-03-01 09:14:04
大家都知道由于性能要求,android要求只能在ui线程中更新ui,要想在其他线程中更新ui,大致有4种方式,下面分别使用四种方式来更新一个textview。
1.使用h...
大家都知道由于性能要求,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的方式,希望对大家的学习有所帮助。
上一篇: MySQL存储引擎总结