ProgressDialog を使ってみた

ProgressDilaog は
java.lang.Object
  ↳ android.app.Dialog
    ↳ android.app.AlertDialog
      ↳ android.app.ProgressDialog
を使います

というわけで、ProgressDialog をいじってみた

サンプルプログラム
  1. import android.app.Activity;  
  2. import android.app.ProgressDialog;  
  3. import android.content.DialogInterface;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6.   
  7. public class ProgressDialogTest extends Activity implements Runnable {  
  8.   ProgressDialog progressDialog;  
  9.   Thread thread;  
  10.   
  11.   @Override  
  12.   public void onCreate(Bundle savedInstanceState) {  
  13.     super.onCreate(savedInstanceState);  
  14.     setContentView(R.layout.progressdialogtest);  
  15.   
  16.     progressDialog = new ProgressDialog(this);  
  17.     
  18.     // ProgressDialog のタイトルを設定  
  19.     progressDialog.setTitle("Title");  
  20.   
  21.     // ProgressDialog のメッセージを設定  
  22.     progressDialog.setMessage("Message");  
  23.   
  24.     // ProgressDialog の確定(false)/不確定(true)を設定します  
  25.     progressDialog.setIndeterminate(false);  
  26.   
  27.     // ProgressDialog のスタイルを水平スタイルに設定  
  28.     progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  29.     // 円スタイルの場合  
  30.     // progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  31.   
  32.     // ProgressDialog の最大値を設定 (水平の時)  
  33.     progressDialog.setMax(100);  
  34.   
  35.     // ProgressDialog の初期値を設定 (水平の時)  
  36.     progressDialog.incrementProgressBy(0);  
  37.   
  38.     // ProgressDialog のセカンダリ値を設定 (水平の時)  
  39.     progressDialog.incrementSecondaryProgressBy(50);  
  40.   
  41.     // ProgressDialog のキャンセルが可能かどうか  
  42.     progressDialog.setCancelable(false);  
  43.    
  44.     // ProgressDialog のキャンセルされた時に呼び出されるコールバックを登録  
  45.     progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {  
  46.       public void onCancel(DialogInterface dialog) {  
  47.         // Thread を停止  
  48.         stop();  
  49.       }  
  50.     });  
  51.   
  52.     // Start ボタン  
  53.     findViewById(R.id.ProgressDialogTest_Button).setOnClickListener(    
  54.       new View.OnClickListener() {    
  55.         public void onClick(View view) {    
  56.           // ProgressDialog を表示  
  57.           progressDialog.show();  
  58.           // Thread を起動  
  59.           init();  
  60.           start();  
  61.         }    
  62.       }  
  63.     );  
  64.   
  65.     // ProgressDialog の Cancel ボタン  
  66.     progressDialog.setButton(  
  67.       DialogInterface.BUTTON_NEGATIVE,  
  68.       "Cancel",  
  69.       new DialogInterface.OnClickListener() {  
  70.         public void onClick(DialogInterface dialog, int which) {  
  71.           // ProgressDialog をキャンセル  
  72.           dialog.cancel();   
  73.         }  
  74.       }  
  75.     );  
  76.   }  
  77.    
  78.   /** Runnable のプログラム */  
  79.   public void init() {  
  80.     thread = null;  
  81.   }  
  82.   public void start() {  
  83.     if (thread == null) {  
  84.       thread = new Thread(this);  
  85.       thread.start();  
  86.     }  
  87.   }  
  88.   public void run() {  
  89.     int count = 0;  
  90.     Thread thisThread = Thread.currentThread();  
  91.     while (thisThread == thread) {  
  92.       // 100ms毎に Progress Bar を進める  
  93.       progressDialog.setProgress(count++);    
  94.       try {  
  95.         thread.sleep(100);    
  96.       } catch (InterruptedException e) {  
  97.       }  
  98.       if (count >= progressDialog.getMax()) {  
  99.         // Progress が完了  
  100.         break;  
  101.       }  
  102.     }  
  103.     // Progress Dialog を消す  
  104.     progressDialog.dismiss();  
  105.   }  
  106.   public void stop() {  
  107.     thread = null;  
  108.   }  
  109. }  

プログラムを実行すると...

円スタイルのとき

こんな感じ!

1 件のコメント:

  1. これのダイアログを使用しないバージョンを教えていただけないでしょうか?

    返信削除