点击按钮显示ProgressDialog对话框

package com.mydiyprogressdialog;

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 MainActivity extends Activity {
	
	int count = 0;
	private Button button1,button2;
	private ProgressDialog progressDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button1 = (Button)findViewById(R.id.button1);
		button2 = (Button)findViewById(R.id.button2);
		
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO 自动生成的方法存根
				//声明ProgressDialog对象
				progressDialog = new ProgressDialog(MainActivity.this);
				
				//获得标题
				progressDialog.setTitle("圆形进度图标");
				
				//获得图标
				progressDialog.setIcon(R.drawable.ic_launcher);
				
				//进度样式,圆形
				progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				
				//获得内容
				progressDialog.setMessage("请稍等...");
				
				//设置ProgressDialog 的进度条是否不明确
				progressDialog.setIndeterminate(true);
				
				//设置ProgressDialog 是否可以按退回按键取消
				progressDialog.setCancelable(true);
				
				//点击确定按钮
				progressDialog.setButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO 自动生成的方法存根
						//点击确定关闭ProgressDialog效果
						dialog.cancel();
					}
				});
				//显示ProgressDialog对话框
				progressDialog.show();
			}
		});
		
		button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				count = 0;
				progressDialog = new ProgressDialog(MainActivity.this);
				progressDialog.setTitle("长形进度条");
				progressDialog.setMessage("请稍等...");
				progressDialog.setIcon(R.drawable.ic_launcher);
				progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				progressDialog.setIndeterminate(false);
				progressDialog.setCancelable(true);
				progressDialog.setProgress(100);
				progressDialog.show();
				
				new Thread(){
					public void run(){
						try{
							while(count <= 100){
								progressDialog.setProgress(count++);
								Thread.sleep(100);
							}
							progressDialog.cancel();
						}catch(InterruptedException e){
							progressDialog.cancel();
						}
					}
				}.start();
				
			}
		});
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值