runOnUiThread 是Android开发中一个重要的方法,可以在非UI线程中切换到UI线程来执行一些UI相关的操作。
Activity源码中runOnUiThread方法:
final Handler mHandler = new Handler();private Thread mUiThread;// ...public final void runOnUiThread(Runnable action) { if (Thread.currentThread() != mUiThread) { mHandler.post(action); } else { action.run(); }// ...}
public class MainActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); textView = findViewById(R.id.tv_test); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } // 更新UI runOnUiThread(new Runnable() { @Override public void run() { textView.setText("更新后的文本"); } }); } }).start(); }}
本文链接:http://www.28at.com/showinfo-26-94435-0.htmlActivity.runOnUiThread方法理解,你学会了吗?
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com