from:http://byandby.iteye.com/blog/817214
顯然要定義對話框進度條就要用ProgressDialog,首先我們需要創建ProgressDialog對象,當然這里同樣使用了線程來控制進度條顯示,另外可以使用以下方法來設置ProgressDialog。
setProgressStyle:設置進度條風格,風格為圓形,旋轉的。
setTitlt:設置ProgressDialog 標題
setMessage:設置ProgressDialog提示信息;
setIcon:設置ProgressDialog標題圖標;
setIndeterminate:設置ProgressDialog 的進度條是否不明確;
setCancelable:設置ProgressDialog 是否可以按返回鍵取消;
setButton:設置ProgressDialog 的一個Button(需要監聽Button事件);
show:顯示ProgressDialog。
首先還是讓我們看看運行效果吧
下面先來看看布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="圓形進度條"/>
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="長形進度條"/>
</LinearLayout>
Activity01
- package xiaohang.zhimeng;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class Activity01 extends Activity {
- private Button xhButton01, xhButton02;
- int xh_count = 0;
- // 聲明進度條對話框
- ProgressDialog xh_pDialog;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- // 得到按鈕對象
- xhButton01 = (Button) findViewById(R.id.Button01);
- xhButton02 = (Button) findViewById(R.id.Button02);
- // 設置xhButton01的事件監聽
- xhButton01.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 創建ProgressDialog對象
- xh_pDialog = new ProgressDialog(Activity01.this);
- // 設置進度條風格,風格為圓形,旋轉的
- xh_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- // 設置ProgressDialog 標題
- xh_pDialog.setTitle("提示");
- // 設置ProgressDialog提示信息
- xh_pDialog.setMessage("這是一個圓形進度條對話框");
- // 設置ProgressDialog標題圖標
- xh_pDialog.setIcon(R.drawable.img1);
- // 設置ProgressDialog 的進度條是否不明確 false 就是不設置為不明確
- xh_pDialog.setIndeterminate(false);
- // 設置ProgressDialog 是否可以按退回鍵取消
- xh_pDialog.setCancelable(true);
- // 設置ProgressDialog 的一個Button
- xh_pDialog.setButton("確定", new Bt1DialogListener());
- // 讓ProgressDialog顯示
- xh_pDialog.show();
- }
- });
- // 設置xhButton02的事件監聽
- xhButton02.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- xh_count = 0;
- // 創建ProgressDialog對象
- xh_pDialog = new ProgressDialog(Activity01.this);
- // 設置進度條風格,風格為圓形,旋轉的
- xh_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- // 設置ProgressDialog 標題
- xh_pDialog.setTitle("提示");
- // 設置ProgressDialog提示信息
- xh_pDialog.setMessage("這是一個長形進度條對話框");
- // 設置ProgressDialog標題圖標
- xh_pDialog.setIcon(R.drawable.img2);
- // 設置ProgressDialog 的進度條是否不明確 false 就是不設置為不明確
- xh_pDialog.setIndeterminate(false);
- // 設置ProgressDialog 進度條進度
- xh_pDialog.setProgress(100);
- // 設置ProgressDialog 是否可以按退回鍵取消
- xh_pDialog.setCancelable(true);
- // 讓ProgressDialog顯示
- xh_pDialog.show();
- new Thread() {
- @Override
- public void run() {
- try {
- while (xh_count <= 100) {
- // 由線程來控制進度
- xh_pDialog.setProgress(xh_count++);
- Thread.sleep(100);
- }
- xh_pDialog.cancel();
- } catch (Exception e) {
- xh_pDialog.cancel();
- }
- }
- }.start();
- }
- });
- }
- // xhButton01的監聽器類
- class Bt1DialogListener implements DialogInterface.OnClickListener {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // 點擊“確定”按鈕取消對話框
- dialog.cancel();
- }
- }
- }