本篇文章给大家分享的是有关android应用中怎么对UI进行异步更新,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
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进行异步更新,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。