程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> Android ProgressBar進度條和ProgressDialog進度框的展示DEMO

Android ProgressBar進度條和ProgressDialog進度框的展示DEMO

編輯:PHP綜合
在做手機開發時,經常碰到一些比較耗時的操作,這個時候進度條就開始派上用場了。
這個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<=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示例
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved