注意:
1、ProgressDialog.STYLE_SPINNER为圆形不确定进度条
2、ProgressDialog.STYLE_HORIZONTAL为条形进图条,当设置为可确定进度的进度条时,在调用show()函数之前设置进度无效,百分比始终为0。第二进度可设置,但是在show()之前设置没什么意义。不管是静态显示抑或是动态显示,都要在show()函数调用之后再操作。
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class ProgressDialogTest extends Activity {
private RelativeLayout mainView=null;
private Button button1=null;
private Button button2=null;
private ProgressDialog pd1=null;
private ProgressDialog pd2=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainView=new RelativeLayout(this);
button1=new Button(this);
RelativeLayout.LayoutParams lp1=new RelativeLayout.LayoutParams(-2,-2);
lp1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
button1.setLayoutParams(lp1);
button1.setText("圆形进度条");
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(pd1==null){
pd1_init();
}
pd1.show();
}
});
mainView.addView(button1);
button2=new Button(this);
RelativeLayout.LayoutParams lp2=new RelativeLayout.LayoutParams(-2,-2);
lp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button2.setLayoutParams(lp2);
button2.setText("条形进度条");
button2.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(pd2==null){
pd2_init();
}
pd2.show();
pd2.setProgress(50);
/*一种动态加载效果
new Thread() {
int progress=0;
public void run() {
try {
while (progress <= 100) {
// 由线程来控制进度
pd2.setProgress(progress++);
Thread.sleep(100);
}
pd2.cancel();
} catch (Exception e) {
pd2.cancel();
}
}
}.start();
*/
}
});
mainView.addView(button2);
setContentView(mainView);
}
/*pd1初始化*/
void pd1_init(){
pd1=new ProgressDialog(this);
pd1.setIcon(android.R.drawable.ic_menu_info_details);
pd1.setTitle("正在联网");
pd1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd1.setMessage("请稍后……");
}
/*pd2初始化*/
void pd2_init(){
pd2=new ProgressDialog(this);
pd2.setIcon(android.R.drawable.sym_def_app_icon);
pd2.setTitle("正在下载软件");
pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd2.setIndeterminate(false);
pd2.setMessage("测试");
pd2.setMax(100);
//pd2.setProgress(50); 在show()之前设置该值无效,show()之前固定进度为0
//pd2.setSecondaryProgress(75);//在show()之前有效,但是意义不大
}
}