欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android ProgressBar进度条和ProgressDialog进度框的展示DEMO

程序员文章站 2022-06-20 13:20:46
在做手机开发时,经常碰到一些比较耗时的操作,这个时候进度条就开始派上用场了。这个demo展示了progressbar进度条和progressdialog进度框。一、prog...
在做手机开发时,经常碰到一些比较耗时的操作,这个时候进度条就开始派上用场了。
这个demo展示了progressbar进度条和progressdialog进度框。
一、progressdialog进度框,效果如图所示:
Android ProgressBar进度条和ProgressDialog进度框的展示DEMO
代码如下:
复制代码 代码如下:

//进度对话框按钮监听
     class prossbuttonlistener implements onclicklistener {
         @override
         public void onclick(view v) {
             mydialog = progressdialog.show(progressbardemo.this, "进度条标题",
                     "进度条内容", true);

             new thread() {
                 public void run() {
                     try {
                         /* 在这里写上要背景运行的程序片段 */
                         /* 为了明显看见效果,以暂停3秒作为示范 */
                         sleep(3000);
                     } catch (exception e) {
                         e.printstacktrace();
                     } finally {
                         // 卸载所创建的mydialog对象。
                         mydialog.dismiss();
                     }
                 }
             }.start(); /* 开始运行运行线程 */
         }
     }

二、进度条对话框,这里用两种情况来动态显示进度条刻度
1、handle方法
效果图如下:
Android ProgressBar进度条和ProgressDialog进度框的展示DEMO
代码如下:
复制代码 代码如下:

//进度条handle按钮监听
     class prossbarhandlebuttonlistener implements onclicklistener {
         @override
         public void onclick(view v) {
             progressbarhandle.setvisibility(view.visible);
             myprossbarhandletext.setvisibility(view.visible);
             progressbarhandle.setmax(1000);
             new thread() {
                 public void run() {
                     for(int i=0;i<=1000;){
                     try {
                         /* 在这里写上运行的进度条 */
                         message msg = new message();
                         msg.what = 1;
                         msg.getdata().putint("size", i);
                         handler.sendmessage(msg);//handle发送消息
                         /* 为了明显看见效果,以暂停1秒作为示范 */
                         sleep(100);
                         i+=10;
                     } catch (exception e) {
                         handler.obtainmessage(-1).sendtotarget();
                         e.printstacktrace();
                     } 
                 }
                 }
             }.start(); /* 开始运行运行线程 */
         }
     }

     //handle接收消息
      private handler handler = new handler(){

             @override
             public void handlemessage(message msg) {           
                 switch (msg.what) {
                 case 1:               
                     progressbarhandle.setprogress(msg.getdata().getint("size"));
                     float num = (float)progressbarhandle.getprogress()/(float)progressbarhandle.getmax();
                     int result = (int)(num*100);
                     system.out.println("progressbarhandle.getprogress()======="+progressbarhandle.getprogress());
                     myprossbarhandletext.settext(result+ "%");
                     if(progressbarhandle.getprogress()==progressbarhandle.getmax()){
                         toast.maketext(progressbardemo.this, "下载成功", 1).show();
                         progressbarhandle.setvisibility(view.gone);
                         myprossbarhandletext.setvisibility(view.gone);
                     }
                     break;

                 case -1:
                     toast.maketext(progressbardemo.this,"下载失败", 1).show();
                     break;
                 }
             }
         };

2、使用asynctask方法,效果图与handle效果一样
具体代码如下:
复制代码 代码如下:

//进度条synctask按钮监听
     class prossbarsyncbuttonlistener implements onclicklistener {
         @override
         public void onclick(view v) {
              new asyncloader().execute((void)null); 
         }
     }

      
      //asynctask 任务执行
     class asyncloader extends asynctask<void, integer, integer> {
         @override
 //         在doinbackground方法执行前执行
         protected void onpreexecute(){
             progressbarhandle.setvisibility(view.visible);
             myprossbarhandletext.setvisibility(view.visible);
             progressbarhandle.setmax(100000);
         }

 //        做具体的耗时操作
         protected integer doinbackground(void... params) {
             //这里以1万的进度条刻度来显示
             int totalsize = 100000;
             for (int i = 0; i < 100000; ) {
                 publishprogress(i);  //通过推送消息传递数据给onprogressupdate方法执行
                 i+=10;
             }
             return totalsize;
         }
 //        在doinbackground方法执行过程中执行
         protected void onprogressupdate(integer... progress) {
             progressbarhandle.setprogress(progress[0]); 
             float num = (float)progressbarhandle.getprogress()/(float)progressbarhandle.getmax();
             int result = (int)(num*100);
              myprossbarhandletext.settext(result+ "%");
         }

 //        在doinbackground方法结束后执行
         protected void onpostexecute(integer result) {
             toast.maketext(progressbardemo.this, "下载成功,下载了"+result, 1).show();
             myprossbarhandletext.setvisibility(view.gone);
             progressbarhandle.setvisibility(view.gone);
         }
     }

点击下载demo示例