• Android在子线程中更新UI(一)


    MainActivity如下:

    package cc.testui1;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.app.Activity;
    /**
     * Demo描述:
     * 在子线程中更改UI的方式一
     * 
     * 在子线程中利用主线程的Handler的post()方法
     * 更改UI这个在子线程中sendMessage()原理和
     * 形式都很类似.
     * 
     * 参考资料:
     * http://blog.csdn.net/guolin_blog/article/details/9991569
     * Thank you very much
     */
    
    public class MainActivity extends Activity {
        private TextView mTextView;
        private Handler mHandler;
        private Button mButton;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		init();
    	}
        private void init(){
        	mHandler=new Handler();
        	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() {
    	    			mHandler.post(new Runnable() {
    						@Override
    						public void run() {
    							mTextView.setText("My 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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="测试在子线程中更新UI" 
            android:layout_centerInParent="true"
            />
    
    </RelativeLayout>


     

  • 相关阅读:
    三代测序及基于三代数据的基因组组装流程评估
    组装技术的新进展 New advances in sequence assembly.
    细菌完成图组装软件简单介绍 细菌
    个人基因组测序将进入千美元费用时代
    HALC:用于长读取错误纠正的高吞吐量算法
    基因组装配新前沿:长片段完成完整的基因组
    第三代PacBio测序技术的测序原理和读长
    三代组装小基因组研究综述
    矩阵连乘 动态规划
    poj 1723 中位数
  • 原文地址:https://www.cnblogs.com/riskyer/p/3283619.html
Copyright © 2020-2023  润新知