Android子线程更新UI两种方法

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

大家都知道直接在子线程里更新UI是不被允许的,所以下面提供两种方法解决问题:

直接上代码:

    package com.wr.updateui;  
      
    import android.annotation.SuppressLint;  
    import android.app.Activity;  
    import android.os.Bundle;  
    import android.os.Handler;  
    import android.os.Message;  
    import android.view.Menu;  
    import android.view.MenuItem;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.widget.Button;  
    import android.widget.TextView;  
      
    public class MainActivity extends Activity {  
      
        private static final int UPDATE_TEXT = 1;  
        private TextView tv;  
        private Button btn;  
        @SuppressLint("HandlerLeak")  
        private Handler handler = new Handler() {  
            public void handleMessage(Message msg) {  
                switch (msg.what) {  
                case UPDATE_TEXT:  
                    tv.setText("nihao");  
                default:  
                    break;  
                }  
            }  
        };  
      
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
      
            tv = (TextView) findViewById(R.id.textView);  
            btn = (Button) findViewById(R.id.button);  
            btn.setOnClickListener(new OnClickListener() {  
      
                @Override  
                public void onClick(View arg0) {  
                    // TODO Auto-generated method stub  
                      
                    /**  
                     * 第一种使用Android的异步处理机制。 
                     */  
                      
                    /* 
                    new Thread(new Runnable() { 
     
                        @Override 
                        public void run() { 
                            // TODO Auto-generated method stub 
                            Message message = new Message(); 
                            message.what = UPDATE_TEXT; 
                            handler.sendMessage(message); 
                        } 
                    }).start(); 
                    */  
                      
                    /**  
                     * 第二种,直接使用runOnUiThread. 
                     */  
                    runOnUiThread(new Runnable() {  
                          
                        @Override  
                        public void run() {  
                            // TODO Auto-generated method stub  
                            tv.setText("nihao");  
                        }  
                    });  
                      
                }  
            });  
        }  
      
        @Override  
        public boolean onCreateOptionsMenu(Menu menu) {  
            // Inflate the menu; this adds items to the action bar if it is present.  
            getMenuInflater().inflate(R.menu.main, menu);  
            return true;  
        }  
      
        @Override  
        public boolean onOptionsItemSelected(MenuItem item) {  
            // Handle action bar item clicks here. The action bar will  
            // automatically handle clicks on the Home/Up button, so long  
            // as you specify a parent activity in AndroidManifest.xml.  
            int id = item.getItemId();  
            if (id == R.id.action_settings) {  
                return true;  
            }  
            return super.onOptionsItemSelected(item);  
        }  
    }