zoukankan      html  css  js  c++  java
  • android UI跨线程操作

     android应用程序也是单线程程序,主线程为UI线程。

     android 线程是非安全的,即不要在子线程中更新 UI。

    • public class MasterActivity extends Activity {  
    •     TextView tv = null;  
    •     Button btn = null;  
    •       
    •     Handler mHandler = new Handler() {  
    •         @Override  
    •         public void handleMessage(Message msg) {  
    •             if(msg.what == 1) {  
    •                 tv.setText("update UI is success!");  
    •                 btn.setText("update UI is success!");  
    •             }  
    •             super.handleMessage(msg);  
    •         }  
    •     };  
    •       
    •     @Override  
    •     public void onCreate(Bundle savedInstanceState) {  
    •         super.onCreate(savedInstanceState);  
    •         setContentView(R.layout.main);  
    •         System.out.println(Thread.currentThread().getName() + ": " + Thread.currentThread().getId());  
    •         tv = (TextView)findViewById(R.id.text);  
    •         btn = (Button)findViewById(R.id.btn);  
    •         btn.setOnClickListener(new OnClickListener() {  
    •               
    •             @Override  
    •             public void onClick(View v) {  
    •                 Thread thread = new Thread(new Runnable() {  
    •                       
    •                     @Override  
    •                     public void run() {  
    •                         System.out.println(Thread.currentThread().getName() + ": " + Thread.currentThread().getId());  
    •                         Message msg = mHandler.obtainMessage();  
    •                         msg.what = 1;  
    •                         msg.sendToTarget();  
    •                     }});  
    •                 thread.start();  
    •             }  
    •         });  
  • 相关阅读:
    微软企业库5.0学习笔记实战数据验证模块高级篇
    总结一些常用的CMS
    JS类库
    sql2
    前端开发必须知道的CSS
    JS实现非图片动态loading
    Microsoft SQL Server 2005 提供了一些工具来监控数据库
    js实现Tooltip
    Js动画基础
    仿iGoogle自定义首页模块拖拽
  • 原文地址:https://www.cnblogs.com/fx2008/p/3140642.html
Copyright © 2011-2022 走看看