zoukankan      html  css  js  c++  java
  • Handler具体解释系列(七)——Activity.runOnUiThread()方法具体解释

    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>


  • 相关阅读:
    记录下IE7下的input标签bug
    CYQ.Data 轻量数据访问层(一) 概述
    网站安装打包 软件环境检测与安装[二] 上
    记录下关于调用RAR解压缩的问题
    CYQ.Data 轻量数据访问层(六) 构造数据表
    记绕过路由封杀问题
    MapXtreme 2005 学习心得 一些问题(八)
    网站安装打包 修改app.config[六]
    关于控件导出Excel格式问题的新解决方案
    CYQ.Data 轻量数据访问层(二) 构造数据单元(上)
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6727411.html
Copyright © 2011-2022 走看看