Android ProgressBar进度条和ProgressDialog进度框的展示DEMO_PHP教程

WBOY
Lepaskan: 2016-07-21 15:05:29
asal
986 orang telah melayarinya

在做手机开发时,经常碰到一些比较耗时的操作,这个时候进度条就开始派上用场了。
这个demo展示了ProgressBar进度条和ProgressDialog进度框。
一、ProgressDialog进度框,效果如图所示:

代码如下:

复制代码 代码如下:

//进度对话框按钮监听
     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方法
效果图如下:

代码如下:
复制代码 代码如下:

//进度条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                     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 {
         @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                  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示例

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/327687.htmlTechArticle在做手机开发时,经常碰到一些比较耗时的操作,这个时候进度条就开始派上用场了。 这个demo展示了ProgressBar进度条和ProgressDialog进度框。...
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan