佈局文件:java
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/cricle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="圓形進度條測試" /> <Button android:id="@+id/rec" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="長形進度條測試" /> </LinearLayout>
測試代碼入口: android
package com.example.progressdialog; import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button circle; private Button rec; private ProgressDialog myDialog; int count = 0;// 存儲進度條當前值,初始爲 0 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 獲取對象 circle = (Button) findViewById(R.id.cricle); rec = (Button) findViewById(R.id.rec); // 圓形按鈕測試 circle.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { circle(); } }); // 矩形進度條測試 rec.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { rec(); } }); } /** * 圓形進度條測試.. */ public void circle() { myDialog = new ProgressDialog(MainActivity.this); // 獲取對象 myDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // 設置樣式爲圓形樣式 myDialog.setTitle("友情提示"); // 設置進度條的標題信息 myDialog.setMessage("數據加載中,請稍後..."); // 設置進度條的提示信息 myDialog.setIcon(R.drawable.ic_launcher); // 設置進度條的圖標 myDialog.setIndeterminate(false); // 設置進度條是否爲不明確 myDialog.setCancelable(true); // 設置進度條是否按返回鍵取消 // 爲進度條添加肯定按鈕 , 並添加單機事件 myDialog.setButton("肯定", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { myDialog.cancel(); // 撤銷進度條 } }); myDialog.show(); // 顯示進度條 } /** * 矩形進度條測試... */ public void rec() { myDialog = new ProgressDialog(MainActivity.this); // 獲得一個對象 myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 設置爲矩形進度條 myDialog.setTitle("提示"); myDialog.setMessage("數據加載中,請稍後..."); myDialog.setIcon(R.drawable.ic_launcher); myDialog.setIndeterminate(false); // 設置進度條是否爲不明確 myDialog.setCancelable(true); myDialog.setMax(200); // 設置進度條的最大值 myDialog.setProgress(0); // 設置當前默認進度爲 0 myDialog.setSecondaryProgress(1000); // 設置第二條進度值爲100 // 爲進度條添加取消按鈕 myDialog.setButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { myDialog.cancel(); } }); myDialog.show(); // 顯示進度條 new Thread() { public void run() { while (count <= 200) { myDialog.setProgress(count++); try { Thread.sleep(100); //暫停 0.1秒 } catch (Exception e) { Log.i("msg","線程異常.."); } } } }.start(); } }
文章來源:http://sunzone.iteye.com/blog/1998095app