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

    package tw.android;
    
    import java.util.Calendar;
    
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.widget.Button;
    
    public class Main extends Activity {
    
        private Button mBtnProgDlg;
        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() {
            mBtnProgDlg = (Button)findViewById(R.id.btnProgDlg);
            mBtnProgDlg.setOnClickListener(btnProgDlgOnClkLis);
        }
    
        private Button.OnClickListener btnProgDlgOnClkLis = new Button.OnClickListener() {
            public void onClick(View v) {
                final ProgressDialog progDlg = new ProgressDialog(Main.this);
                progDlg.setTitle("进度条");
                progDlg.setMessage("进度...");
                progDlg.setIcon(android.R.drawable.ic_dialog_info);
                progDlg.setCancelable(false);
                progDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progDlg.setMax(100);
                progDlg.show();
                
                new Thread(new Runnable() {
                     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() {
                                            progDlg.setProgress(100);
                                        }
                                    });
                                    
                                    break;
                                }
                                
                                mHandler.post(new Runnable() {
                                    public void run() {
                                        progDlg.setProgress(iDiffSec * 2);
                                    }
                                });
                                
                                if (iDiffSec * 4 < 100)
                                    mHandler.post(new Runnable() {
                                        public void run() {
                                            progDlg.setSecondaryProgress(iDiffSec * 4);
                                        }
                                    });
                                else
                                    mHandler.post(new Runnable() {
                                        public void run() {
                                            progDlg.setSecondaryProgress(100);
                                        }
                                    });
                            } while (true);
                            
                            progDlg.cancel();
                       }
                }).start();
            }
        };
    }
    以上是ProgressDialog代码,ProgressDialog 是需要的时候才显示,工作完后就消失。
  • 相关阅读:
    作业十三
    作业十二
    第十一次作业
    编译原理第十次作业
    P3388 【模板】割点(割顶) 题解 (Tarjan)
    BuaacodingT141 microhhh的回城 题解(模拟)
    P2055 [ZJOI2009]假期的宿舍 题解(二分图)
    P2764 最小路径覆盖问题 题解(二分图)
    2019.2-2019.3 TO-DO LIST
    P3369 【模板】普通平衡树 题解(Splay/FHQ)
  • 原文地址:https://www.cnblogs.com/zhoujn/p/4155022.html
Copyright © 2011-2022 走看看