进度条弹窗

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

	ProgressDialog pd;
	RelativeLayout rl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		pd = new ProgressDialog(MainActivity.this);
		rl = (RelativeLayout) findViewById(R.id.main);
		showHor();
//		showAll();
	}
	
	/**
	 * 显示横向的进度条
	 */
	public void showHor(){
		pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		pd.setIcon(R.drawable.ic_launcher);
		pd.setTitle("ProgressDialog");
		pd.setMessage("This is the message that needs to be displayed");
		pd.setProgress(0);
		pd.setIndeterminate(false);//显示具体刻度
		pd.setCancelable(false);//返回键不能取消
		pd.show();
		new Thread(new Runnable() {
			public void run() {
				boolean show = true;
				int c = 0;
				while (show) {
					if (c <= 100) {
						try {
							Thread.sleep(100);
						} catch (Exception e) {
							// TODO: handle exception
						}
						c++;
						handler.obtainMessage(100, c).sendToTarget();
					}else {
						handler.obtainMessage(101).sendToTarget();
						show = false;
					}
				}
			}
		}).start();
	}
	
	/**
	 * 显示圆形进度条
	 */
	public void showAll(){
		pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//		pd.setIcon(R.drawable.ic_launcher);
//		pd.setTitle("ProgressDialog");
		pd.setMessage("loading...");
		pd.setProgress(0);
		pd.setIndeterminate(false);
		pd.setCancelable(false);
		pd.show();
		new Thread(new Runnable() {
			public void run() {
				boolean show = true;
				int c = 0;
				while (show) {
					if (c <= 100) {
						try {
							Thread.sleep(100);
						} catch (Exception e) {
							// TODO: handle exception
						}
						c++;
						handler.obtainMessage(100, c).sendToTarget();
					}else {
						handler.obtainMessage(101).sendToTarget();
						show = false;
					}
				}
			}
		}).start();
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 100:
				int progress = Integer.parseInt(msg.obj.toString());
				if (pd != null) {
					pd.setMessage("loading... ( " + progress + "% )");
					pd.setProgress(progress);
				}
				break;
			case 101:
				if (pd != null) {
					pd.cancel();
				}
				break;
			}
		};
	};

}

猜你喜欢

转载自blog.csdn.net/qq_24179679/article/details/77646251