標籤:des android style blog http io color ar sp
1 /** 2 * Description: 3 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> 4 * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee 5 * <br/>This program is protected by copyright laws. 6 * <br/>Program Name: 7 * <br/>Date: 8 * @author Yeeku.H.Lee [email protected] 9 * @version 1.0 10 */ 11 public class ProgressDialogTest extends Activity 12 { 13 // 該程式類比填充長度為100的數組 14 private int[] data = new int[100]; 15 int hasData = 0; 16 // 定義進度對話方塊的標識 17 final int PROGRESS_DIALOG = 0x112; 18 // 記錄進度對話方塊的完成百分比 19 int progressStatus = 0; 20 ProgressDialog pd; 21 // 定義一個負責更新的進度的Handler 22 Handler handler; 23 24 @Override 25 public void onCreate(Bundle savedInstanceState) 26 { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.main); 29 Button execBn = (Button) findViewById(R.id.exec); 30 execBn.setOnClickListener(new OnClickListener() 31 { 32 public void onClick(View source) 33 { 34 showDialog(PROGRESS_DIALOG); 35 } 36 }); 37 handler = new Handler() 38 { 39 @Override 40 public void handleMessage(Message msg) 41 { 42 // 表明訊息是由該程式發送的。 43 if (msg.what == 0x111) 44 { 45 pd.setProgress(progressStatus); 46 } 47 } 48 }; 49 } 50 51 @Override 52 public Dialog onCreateDialog(int id, Bundle status) 53 { 54 System.out.println("------create------"); 55 switch (id) 56 { 57 case PROGRESS_DIALOG: 58 // 建立進度對話方塊 59 pd = new ProgressDialog(this); 60 pd.setMax(100); 61 // 設定對話方塊的標題 62 pd.setTitle("任務完成百分比"); 63 // 設定對話方塊 顯示的內容 64 pd.setMessage("耗時任務的完成百分比"); 65 // 設定對話方塊不能用“取消”按鈕關閉 66 pd.setCancelable(false); 67 // 設定對話方塊的進度條風格 68 pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); //圓形進度條 69 // pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//水平進度條 70 // 設定對話方塊的進度條是否顯示進度 71 pd.setIndeterminate(false); 72 break; 73 } 74 return pd; 75 } 76 77 // 該方法將在onCreateDialog方法調用之後被回調 78 @Override 79 public void onPrepareDialog(int id, Dialog dialog) 80 { 81 System.out.println("------prepare------"); 82 super.onPrepareDialog(id, dialog); 83 switch (id) 84 { 85 case PROGRESS_DIALOG: 86 // 對話方塊進度清零 87 pd.incrementProgressBy(-pd.getProgress()); 88 new Thread() 89 { 90 public void run() 91 { 92 while (progressStatus < 100) 93 { 94 // 擷取耗時操作的完成百分比 95 progressStatus = doWork(); 96 // 發送訊息到Handler 97 Message m = new Message(); 98 m.what = 0x111; 99 // 發送訊息100 handler.sendMessage(m);101 }102 103 // 如果任務已經完成104 if (progressStatus >= 100)105 {106 // 關閉對話方塊107 pd.dismiss();108 }109 }110 }.start();111 break;112 }113 }114 115 // 類比一個耗時的操作。116 public int doWork()117 {118 // 為數組元素賦值119 data[hasData++] = (int) (Math.random() * 100);120 try121 {122 Thread.sleep(100);123 }124 catch (InterruptedException e)125 {126 e.printStackTrace();127 }128 return hasData;129 }130 }
android ProgressDialog 對話方塊