Android異步更新UI的四種方式
大家都知道由于性能要求,android要求只能在UI線程中更新UI,要想在其他線程中更新UI,大致有4種方式,下面分別使用四種方式來(lái)更新一個(gè)TextView。
1.使用Handler消息傳遞機(jī)制
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異步任務(wù)(更新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的方式,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android Studio中導(dǎo)入module的方法(簡(jiǎn)單版)
這篇文章主要介紹了AndroidStudio中導(dǎo)入module的方法,本文是一篇簡(jiǎn)易版的教程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01Android?廣播接收器BroadcastReceiver詳解
Android開發(fā)的四大組件分別是:活動(dòng)(activity),用于表現(xiàn)功能;服務(wù)(service),后臺(tái)運(yùn)行服務(wù),不提供界面呈現(xiàn);廣播接受者(Broadcast Receive),勇于接收廣播;內(nèi)容提供者(Content Provider),支持多個(gè)應(yīng)用中存儲(chǔ)和讀取數(shù)據(jù),相當(dāng)于數(shù)據(jù)庫(kù),本篇著重介紹廣播組件2022-07-07Android中Activity跳轉(zhuǎn)的創(chuàng)建步驟總結(jié)
這篇文章主要介紹了Android中Activity跳轉(zhuǎn)的創(chuàng)建步驟總結(jié),本文詳細(xì)的講解了從工程創(chuàng)建到跳轉(zhuǎn)Activity的實(shí)現(xiàn)完整過程,需要的朋友可以參考下2014-10-10Android Socket通信的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android Socket通信的簡(jiǎn)單實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Android實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)功能示例
本篇文章主要介紹了Android實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)功能示例,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-03-03Android簡(jiǎn)單實(shí)現(xiàn)自定義流式布局的方法
這篇文章主要介紹了Android簡(jiǎn)單實(shí)現(xiàn)自定義流式布局的方法,結(jié)合實(shí)例形式分析了Android流式布局的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07Flutter實(shí)現(xiàn)漸變弧形進(jìn)度條的示例詳解
在Flutter開發(fā)中,構(gòu)建一個(gè)具有視覺吸引力的、反映進(jìn)度的圓形弧形進(jìn)度條是一個(gè)常見需求,本文將詳細(xì)介紹如何使用Flutter和Dart語(yǔ)言實(shí)現(xiàn)這一功能,需要的可以參考下2023-12-12Android從Fragment跳轉(zhuǎn)到其他Activity的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android從Fragment跳轉(zhuǎn)到其他Activity的簡(jiǎn)單實(shí)例,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02