且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Handler详解系列(七)——Activity.runOnUiThread()方法详解

更新时间:2022-04-08 17:17:34

MainActivity如下:
package cc.testui3;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
/**
 * Demo描述:
 * 在子线程中更改UI的方式三
 * 
 * 调用Activity.runOnUiThread(Runnable runnable)方法.
 * 
 *该方法的源码如下:
 *
 * Runs the specified action on the UI thread. If the current thread is the UI
 * thread, then the action is executed immediately. If the current thread is
 * not the UI thread, the action is posted to the event queue of the UI thread.
 *
 * @param action the action to run on the UI thread
 * public final void runOnUiThread(Runnable action) {
 *    if (Thread.currentThread() != mUiThread) {
 *        mHandler.post(action);
 *    } else {
 *        action.run();
 *      }
 * }
 * 
 * 
 * 源码中的这段注释太详细和贴心了,赞一个!
 * 在UI线程中执行该Runnable.
 * 如果当前线程是UI线程,那么该Runnable会立即执行.
 * 如果当前的线程不是UI线程则调用UI线程handler的post()方法将其放入UI线程的消息队列中.
 * 注意:勿在runOnUiThread(Runnable runnable)中做耗时操作
 * 
 * 参考资料:
 * http://blog.csdn.net/guolin_blog/article/details/9991569
 * Thank you very much
 */
public class MainActivity extends Activity {
	 private TextView mTextView;
	 private Activity mActivity;
	 private Button mButton;
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.main);
			init();
		}
	    private void init(){
	    	mActivity=this;
	    	mTextView=(TextView) findViewById(R.id.textView);
	    	mButton=(Button) findViewById(R.id.button);
	    	mButton.setOnClickListener(new OnClickListenerImpl());
	    }
		
	private class OnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			new Thread(){
				public void run() {
					mActivity.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							mTextView.setText("My name is zxc , number is 007");
						}
					});
				};
			}.start();
		}
	}
	
}

main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

     <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dip"
        />
     
     <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="120dip"
        />
    
    <TextView
        android:id="@+id/tipTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试在子线程中更新UI" 
        android:layout_centerInParent="true"
        />

</RelativeLayout>