首页 > 系统 > Android > 正文

Android UI更新的几种方法总结

2020-02-21 17:22:12
字体:
来源:转载
供稿:网友

随着开发项目的变化,一些入口界面会出现增减、或者内容变化、跳转界面变化等等问题,今天是武林技术频道小编给大家带来的Android UI更新的几种方法总结。

1、Activity的 runOnUiThread  

textView = (TextView) findViewById( R.id.tv );    new Thread(new Runnable() {      @Override      public void run() {         runOnUiThread(new Runnable() {          @Override          public void run() {            textView.setText( "更新UI了");          }        });      }    }).start();

android Activity runOnUiThread() 方法使用 

2、Handler sendEmptyMessage()

package lib.com.myapplication; import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.TextView; public class MainActivity extends AppCompatActivity {   private TextView textView ;   Handler handler = new Handler( ) {    @Override    public void handleMessage(Message msg) {      super.handleMessage(msg);      textView.setText( "Ui更新了");    }  };   @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);     textView = (TextView) findViewById( R.id.tv );    new Thread(new Runnable() {      @Override      public void run() {        try {          Thread.sleep( 2000 );        } catch (InterruptedException e) {          e.printStackTrace();        }         handler.sendEmptyMessage( 2 ) ;      }    }).start();   }}

3、Handler  post()

package lib.com.myapplication;import android.os.Bundle;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.widget.TextView; public class MainActivity extends AppCompatActivity {   private TextView textView ;   Handler handler = new Handler();   @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);     textView = (TextView) findViewById( R.id.tv );     new Thread(new Runnable() {      @Override      public void run() {        try {          Thread.sleep( 2000 );        } catch (InterruptedException e) {          e.printStackTrace();        }         handler.post(new Runnable() {          @Override          public void run() {            textView.setText( "Ui更新了");          }        }) ;      }    }).start();   }}

在子线程中切换到主线程

new Thread(new Runnable() {  @Override  public void run() {    LogUtil.d( "ttt 11111111111" + Thread.currentThread().getName() );    new Handler(Looper.getMainLooper()).post(new Runnable() {      @Override      public void run() {        LogUtil.d( "ttt 55555555" + Thread.currentThread().getName() );      }    });     LogUtil.d( "ttt 22222222222" + Thread.currentThread().getName() );    LogUtil.d( "ttt 33333333333" + Thread.currentThread().getName() );    LogUtil.d( "ttt 44444444444" + Thread.currentThread().getName() );   }}).start();

  结果

ttt 11111111111Thread-155ttt 22222222222Thread-155ttt 33333333333Thread-155ttt 44444444444Thread-155ttt 55555555main

  可见这种方式可以快速切换线程,从log日志来看,切换到主线程不会阻塞子线程。

4、view Post() 

textView = (TextView) findViewById( R.id.tv );    new Thread(new Runnable() {      @Override      public void run() {        try {          Thread.sleep( 2000 );        } catch (InterruptedException e) {          e.printStackTrace();        }         textView.post(new Runnable() {          @Override          public void run() {            textView.setText( "Ui更新了");          }        }) ;      }    }).start();

上述是武林技术频道小编为大家介绍的Android UI更新的几种方法总结,希望对你学习这方面知识有所帮助,也希望大家能一如既往的支持我们。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表