Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 開發入門 >> Android ProgressDialog簡潔處理裝置

Android ProgressDialog簡潔處理裝置

編輯:開發入門

ProgressDialog往往通過線程現實進度什麼的,比如大文件拷貝或者網絡訪問下載等,但是用handler和線程搭配顯得比較麻煩,剛閒著無聊寫了個簡潔的方法。


  1. public class main extends Activity {
  2. private static boolean isCannel = false;
  3. private ProgressDialog  progressDialog;
  4. @Override
  5.     public void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentVIEw(R.layout.main);
  8.         
  9.         progressDialog = new ProgressDialog (this);
  10.         CharSequence title = "ProgressDialog監聽線程處理進度";
  11.         //CharSequence message = getString(R.string.xxx);
  12.         CharSequence message = "當前處理進度";
  13.         
  14.         ProcessThread mThread = new ProcessThread();
  15.         progressDialog = new ProgressDialog(this);
  16.         progressDialog.setTitle(title);
  17.         progressDialog.setMessage(message);
  18.         progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  19.         progressDialog.setButton((CharSequence)"取消", mThread);
  20.         progressDialog.setProgress(0);
  21.         progressDialog.setMax(100);
  22.         progressDialog.setOnCancelListener(mThread);
  23.         progressDialog.setOnDismissListener(mThread);
  24.         progressDialog.show();
  25.         mThread.start();
  26.     }
  27.     
  28.     private class ProcessThread extends Thread implements OnCancelListener, OnDismissListener, OnClickListener{
  29.      public void run() {
  30.       isCannel = false;
  31.       int process = 0;
  32.       while(!isCannel && process < 100) {
  33.        progressDialog.incrementProgressBy(1);
  34.        try {
  35.      sleep(200);
  36.     } catch (InterruptedException e) {
  37.      e.printStackTrace();
  38.     }
  39.     process ++;
  40.       }
  41.       progressDialog.dismiss();
  42.       finish();
  43.      }
  44.      
  45.   public void onClick(DialogInterface dialog, int which) {
  46.    isCannel = true;
  47.   }
  48.   public void onDismiss(DialogInterface dialog) {
  49.    isCannel = true;
  50.   }
  51.   public void onCancel(DialogInterface dialog) {
  52.    isCannel = true;
  53.   }
  54.     }
  55. }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved