java.lang.Object ↳ android.app.Dialog ↳ android.app.AlertDialog ↳ android.app.ProgressDialogを使います
というわけで、ProgressDialog をいじってみた
サンプルプログラム
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.View;
- public class ProgressDialogTest extends Activity implements Runnable {
- ProgressDialog progressDialog;
- Thread thread;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.progressdialogtest);
- progressDialog = new ProgressDialog(this);
- // ProgressDialog のタイトルを設定
- progressDialog.setTitle("Title");
- // ProgressDialog のメッセージを設定
- progressDialog.setMessage("Message");
- // ProgressDialog の確定(false)/不確定(true)を設定します
- progressDialog.setIndeterminate(false);
- // ProgressDialog のスタイルを水平スタイルに設定
- progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- // 円スタイルの場合
- // progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- // ProgressDialog の最大値を設定 (水平の時)
- progressDialog.setMax(100);
- // ProgressDialog の初期値を設定 (水平の時)
- progressDialog.incrementProgressBy(0);
- // ProgressDialog のセカンダリ値を設定 (水平の時)
- progressDialog.incrementSecondaryProgressBy(50);
- // ProgressDialog のキャンセルが可能かどうか
- progressDialog.setCancelable(false);
- // ProgressDialog のキャンセルされた時に呼び出されるコールバックを登録
- progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
- public void onCancel(DialogInterface dialog) {
- // Thread を停止
- stop();
- }
- });
- // Start ボタン
- findViewById(R.id.ProgressDialogTest_Button).setOnClickListener(
- new View.OnClickListener() {
- public void onClick(View view) {
- // ProgressDialog を表示
- progressDialog.show();
- // Thread を起動
- init();
- start();
- }
- }
- );
- // ProgressDialog の Cancel ボタン
- progressDialog.setButton(
- DialogInterface.BUTTON_NEGATIVE,
- "Cancel",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- // ProgressDialog をキャンセル
- dialog.cancel();
- }
- }
- );
- }
- /** Runnable のプログラム */
- public void init() {
- thread = null;
- }
- public void start() {
- if (thread == null) {
- thread = new Thread(this);
- thread.start();
- }
- }
- public void run() {
- int count = 0;
- Thread thisThread = Thread.currentThread();
- while (thisThread == thread) {
- // 100ms毎に Progress Bar を進める
- progressDialog.setProgress(count++);
- try {
- thread.sleep(100);
- } catch (InterruptedException e) {
- }
- if (count >= progressDialog.getMax()) {
- // Progress が完了
- break;
- }
- }
- // Progress Dialog を消す
- progressDialog.dismiss();
- }
- public void stop() {
- thread = null;
- }
- }
プログラムを実行すると...
円スタイルのとき
こんな感じ!
これのダイアログを使用しないバージョンを教えていただけないでしょうか?
返信削除