ProgressDialog 类似于ProgressBar,都是用于显示进度,区别是一个是控件一个是对话框。
ProgressDialog有两种形式一个是STYLE_SPINNER,STYLE_HORIZONTAL。
其中前者没有固定值,后者可以设置最大值和当前值。
下面直接给出一个实例。
- package com.rocky.demo;
- 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 ProgressDialogDemo extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ((Button)this.findViewById(R.id.Button01)).setOnClickListener(btn1Clicker);
- ((Button)this.findViewById(R.id.Button02)).setOnClickListener(btn2Clicker);
- }
- int m_count=0;
- ProgressDialog prDialog;
- OnClickListener btn1Clicker=new OnClickListener()
- {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- prDialog=new ProgressDialog(ProgressDialogDemo.this);
- prDialog.setTitle("提示信息");
- prDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- prDialog.setMessage("This is a cicle process dialog ");
- prDialog.setIcon(R.drawable.icon);
- //进度条是否不明确
- prDialog.setIndeterminate(true);
- prDialog.setCancelable(true);
- prDialog.setButton("确定", new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- prDialog.cancel();
- }
- });
- prDialog.show();
- }
- };
- OnClickListener btn2Clicker=new OnClickListener()
- {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- m_count=0;
- prDialog=new ProgressDialog(ProgressDialogDemo.this);
- prDialog.setTitle("提示信息");
- prDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- prDialog.setMessage("This is a cicle process dialog ");
- prDialog.setIcon(R.drawable.icon);
- //进度条是否不明确
- prDialog.setIndeterminate(false);
- prDialog.setCancelable(true);
- prDialog.setProgress(200);
- prDialog.setMax(200);
- prDialog.setButton("确定", new DialogInterface.OnClickListener(){
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- prDialog.cancel();
- }
- });
- prDialog.show();
- new Thread()
- {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- //super.run();
- try
- {
- while(m_count<=200)
- {
- prDialog.setProgress(m_count++);
- Thread.sleep(100);
- }
- prDialog.cancel();
- }
- catch(InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }.start();
- }
- };
- }
<!--EndFragment-->