Android 之 ProgressDialog用法介绍

  布局文件测试: 

<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>

  测试代码入口: 

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();

    }

}

  demo下载地址:http://pan.baidu.com/s/1gddHDh5

原文地址:https://www.cnblogs.com/AndroidJotting/p/4753093.html