APP應用中經常會下載某些東西,這里面有涉及到進度對話框,今天來學習下。
首先,布局里放進兩個按鈕,點擊一個顯示條形進度條,另一個顯示圓形進度條。代碼如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 android:id="@+id/LinearLayout01" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 xmlns:android="http://schemas.android.com/apk/res/android"> 8 9 <Button 10 android:id="@+id/progress" 11 android:layout_width="128dp" 12 android:layout_height="wrap_content" 13 android:text="條形進度條" /> 14 15 <Button 16 android:id="@+id/circle" 17 android:layout_width="128dp" 18 android:layout_height="wrap_content" 19 android:text="圓形進度條" /> 20 21 </LinearLayout>
顯示效果:
修改MainActivity.java文件:
1 package com.example.progressdialog; 2 3 import android.app.Activity; 4 import android.app.ProgressDialog; 5 import android.content.DialogInterface; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Button; 9 10 public class MainActivity extends Activity { 11 12 private Button m_btnProgress = null; 13 private Button m_btnCircle = null; 14 private ProgressDialog pDialog = null; 15 private int iCount = 0; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 m_btnProgress = (Button) findViewById(R.id.progress); 23 m_btnCircle = (Button) findViewById(R.id.circle); 24 25 m_btnProgress.setOnClickListener(new View.OnClickListener() { 26 27 @Override 28 public void onClick(View v) { 29 iCount = 0; 30 pDialog = new ProgressDialog(MainActivity.this); 31 32 // 設置進度條風格,風格為長形 33 pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 34 35 // 設置ProgressDialog 標題 36 pDialog.setTitle("條形進度條"); 37 38 // 設置ProgressDialog 提示信息 39 pDialog.setMessage("正在下載中……"); 40 41 // 設置ProgressDialog 標題圖標 42 pDialog.setIcon(R.drawable.ic_launcher); 43 44 // 設置ProgressDialog 進度條進度 45 pDialog.setProgress(100); 46 47 // 設置ProgressDialog 的進度條是否不明確 48 pDialog.setIndeterminate(false); 49 50 // 設置ProgressDialog 是否可以按退回按鍵取消 51 pDialog.setCancelable(true); 52 53 // 讓ProgressDialog顯示 54 pDialog.show(); 55 56 new Thread() { 57 public void run() { 58 try { 59 while (iCount <= 100) { 60 // 由線程來控制進度。 61 pDialog.setProgress(iCount++); 62 Thread.sleep(80); 63 } 64 pDialog.cancel(); 65 } catch (InterruptedException e) { 66 67 } 68 } 69 }.start(); 70 71 } 72 }); 73 74 m_btnCircle.setOnClickListener(new View.OnClickListener() { 75 76 @Override 77 public void onClick(View v) { 78 79 iCount = 0; 80 // 創建ProgressDialog對象 81 pDialog = new ProgressDialog(MainActivity.this); 82 83 // 設置進度條風格,風格為圓形,旋轉的 84 pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 85 86 // 設置ProgressDialog 標題 87 pDialog.setTitle("圓形進度條"); 88 89 // 設置ProgressDialog 提示信息 90 pDialog.setMessage("正在下載中……"); 91 92 // 設置ProgressDialog 標題圖標 93 pDialog.setIcon(R.drawable.ic_launcher); 94 95 // 設置ProgressDialog 進度條進度 96 pDialog.setProgress(100); 97 98 // 設置ProgressDialog 的進度條是否不明確 99 pDialog.setIndeterminate(false); 100 101 // 設置ProgressDialog 是否可以按退回按鍵取消 102 pDialog.setCancelable(true); 103 104 // 設置ProgressDialog 的一個Button 105 pDialog.setButton("取消", new DialogInterface.OnClickListener() { 106 public void onClick(DialogInterface dialog, int i) { 107 // 點擊“取消”按鈕取消對話框 108 dialog.cancel(); 109 } 110 }); 111 112 // 讓ProgressDialog顯示 113 pDialog.show(); 114 115 // 創建線程實例 116 new Thread() { 117 public void run() { 118 try { 119 while (iCount <= 100) { 120 // 由線程來控制進度。 121 pDialog.setProgress(iCount++); 122 Thread.sleep(80); 123 } 124 pDialog.cancel(); 125 } catch (InterruptedException e) { 126 pDialog.cancel(); 127 } 128 } 129 130 }.start(); 131 132 } 133 }); 134 } 135 136 }
點擊按鈕效果: