第一种方法(比较简单):
package com.example.dialogdemo; import java.util.Random; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.SystemClock; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ProgressDialog progressDialog=new ProgressDialog(this); //进度条设置成水平 progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置最大值 progressDialog.setMax(100); progressDialog.show(); //线程 new Thread(new Runnable() { private int progress; @Override public void run() { // TODO Auto-generated method stub while(true){ progressDialog.setProgress(progress); progress++; SystemClock.sleep(new Random().nextInt(100)); if(progress==100){ progressDialog.dismiss(); } } } }).start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
效果执行图:进入程序只能执行一次,相当于数据的“导入”。
第二中方法(仿Andorid应用的安装):
package com.example.progressdialog; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; public class ProgressDialogActivity extends Activity { //任务大小 private int[] data = new int[100]; //已经完成数据 private int hasdata = 0; //完成程度 private int status = 0; //设置标记 final int SIGN = 0x11; Handler h; //定义一个ProgressDialog ProgressDialog pd ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button)findViewById(R.id.button); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(SIGN); } }); h = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case 0x1111: //设置状态 pd.setProgress(status); break; default: break; } pd.setProgress(status); } }; } //调用onPrepareDialog()方法之后 @Override protected Dialog onCreateDialog(int id, Bundle args) { // TODO Auto-generated method stub pd = new ProgressDialog(this); pd.setMax(100); pd.setMessage("正在安装中"); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setCancelable(false); return pd; } //调用showDialog()之后和调用onCreateDialog之前 @Override protected void onPrepareDialog(int id, Dialog dialog, Bundle args) { // TODO Auto-generated method stub switch (id) { case SIGN: pd.incrementProgressBy(-pd.getProgress()); new Thread(){ @Override public void run() { while(status < 100){ status = dowork(); Message msg = new Message(); msg.what = 0x1111; h.sendMessage(msg); } if(status >= 100){ //撤销 pd.dismiss(); } } }.start(); break; } } public int dowork(){ data[hasdata ++] = (int)(Math.random()*100); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } return hasdata; } }
执行效果图:点击按钮可重复执行