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();
}
});
}
}
点击按钮显示ProgressDialog对话框
最新推荐文章于 2021-05-27 02:04:27 发布