Android入门之ProgressDialog(纯java)

注意:

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()之前有效,但是意义不大
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值