zoukankan      html  css  js  c++  java
  • Android进度条学习一

    DoLengthyWork.java

    package tw.android;
    
    import java.util.Calendar;
    
    import android.os.Handler;
    import android.widget.ProgressBar;
    
    public class DoLengthyWork extends Thread {
    
        private Handler mHandler;//对象mHandler用来运行post更新ProgressBar的工作
        private ProgressBar mProBar;//该对象用来存储要用来处理的ProgressBar
        //用来读取系统时间的循环,并持续post更新ProgressBar的工作。
        public void run () {
            Calendar begin = Calendar.getInstance();//用来读取系统的时间
            do {
                Calendar now = Calendar.getInstance();
                final int iDiffSec = 60 * (now.get(Calendar.MINUTE) - begin.get(Calendar.MINUTE)) +
                                now.get(Calendar.SECOND) - begin.get(Calendar.SECOND);
            
                if (iDiffSec * 2 > 100) {
                    mHandler.post(new Runnable() {
                        public void run() {
                            mProBar.setProgress(100);
                        }
                    });
                    
                    break;
                }
                
                mHandler.post(new Runnable() {
                    public void run() {
                        mProBar.setProgress(iDiffSec * 2);
                    }
                });
                
                if (iDiffSec * 4 < 100)
                    mHandler.post(new Runnable() {
                        public void run() {
                            mProBar.setSecondaryProgress(iDiffSec * 4);
                        }
                    });
                else
                    mHandler.post(new Runnable() {
                        public void run() {
                            mProBar.setSecondaryProgress(100);
                        }
                    });
            } while (true);
        }
        
        void setProgressBar(ProgressBar proBar) {
            mProBar = proBar;
        }
        
        void setHandler(Handler h) {
            mHandler = h;
        }
    }

    Main.java

    package tw.android;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.widget.ProgressBar;
    
    public class Main extends Activity {
    
        private Handler mHandler = new Handler();
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            setupViewComponent();
        }
        
        private void setupViewComponent() {
            // 從資源類別R中取得介面元件
            final ProgressBar proBar = (ProgressBar)findViewById(R.id.proBar2);
    
            DoLengthyWork work = new DoLengthyWork();
            work.setHandler(mHandler);
            work.setProgressBar(proBar);
            work.start();
        }
    }

    在Mina.java中建立一个Handler对象,并且在setupViewComponent()他的内部设置了两个对象。

  • 相关阅读:
    解读《TCP/IP详解》(卷1):05章:RARP(逆地址解析协议)
    Android中利用“反射”动态加载R文件中的资源
    “骆驼”怎么写
    eclipse无法访问genymotion模拟器下/data/data文件解决方案
    解读《TCP/IP详解》(卷1):03章:IP(网际协议)
    解读《TCP/IP详解》(卷1):02章:链路层
    hdu1039 java正则表达式解法
    hdu1027 又是next_permutaiton
    hdu1261 java水过高精度排列组合。。
    hdu1716 STL next_permutation函数的使用
  • 原文地址:https://www.cnblogs.com/zhoujn/p/4155037.html
Copyright © 2011-2022 走看看