//在点击事件里直接new出来用
-
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);//1.创建一个ProgressDialog的实例
-
progressDialog.setTitle("这是一个 progressDialog");//2.设置标题
-
progressDialog.setMessage("正在加载中,请稍等......");//3.设置显示内容
-
progressDialog.setCancelable(true);//4.设置可否用back键关闭对话框
-
progressDialog.show();//5.将ProgessDialog显示出来
//还可以这样简单的写 只有旋转的圆圈(点击事件里调用)
ProgressDialog dialog = new ProgressDialog(this);
dialog.show();
/还可以这样(点击事件里调用)
// 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean indeterminate设置是否是不明确的状态
ProgressDialog dialog3 = ProgressDialog
.show(this, "提示", "正在登陆中", false);
//这样的(点击事件里调用)
// 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数 DialogInterface.OnCancelListener
// cancelListener用于监听进度条被取消
ProgressDialog dialog5 = ProgressDialog.show(this, "提示", "正在登陆中", true,
true, cancelListener);
private DialogInterface.OnCancelListener cancelListener = new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(Main3Activity.this, "进度条被取消", Toast.LENGTH_LONG)
.show();
}
};
//还有这样的圆形不明确状态(点击事件里调用)
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
dialog.setIcon(R.mipmap.ic_launcher);//
// 设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的
dialog.setTitle("提示");
// dismiss监听
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
}
});
// 监听Key事件被传递给dialog
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
return false;
}
});
// 监听cancel事件
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
}
});
//设置可点击的按钮,最多有三个(默认情况下)
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog.setMessage("这是一个圆形进度条");
dialog.show();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(5000);
// cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是
// 调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss方法不会回掉
dialog.cancel();
// dialog.dismiss();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
//还有这样的水平进度条状态
// 进度条还有二级进度条的那种形式,这里就不演示了
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平进度条
dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
dialog.setIcon(R.mipmap.ic_launcher);// 设置提示的title的图标,默认是没有的
dialog.setTitle("提示");
dialog.setMax(100);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
dialog.setMessage("这是一个水平进度条");
dialog.show();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while (i < 100) {
try {
Thread.sleep(200);
// 更新进度条的进度,可以在子线程中更新进度条进度
dialog.incrementProgressBy(1);
// dialog.incrementSecondaryProgressBy(10)//二级进度条更新方式
i++;
} catch (Exception e) {
// TODO: handle exception
}
}
// 在进度条走完时删除Dialog
dialog.dismiss();
}
}).start();
//设置内容和标题居中:
//内容居中:
TextView msg = new TextView(this);
msg.setText("我是自定义的dialog的message");
msg.setPadding(10, 10, 10, 10);
msg.setGravity(Gravity.CENTER);
msg.setTextSize(18);
alert.setView(msg);
alert.setCancelable(false);
alert.show();
//标题居中:
TextView title = new TextView(this);
title.setText("我是自定义的dialog的title");
title.setPadding(10, 10, 10, 10); //边距
title.setGravity(Gravity.CENTER); //位置
title.setTextColor(getResources().getColor(R.color.bg_image)); //字体的颜色
title.setTextSize(23); //字体的大小
alert.setCustomTitle(title); //设置字体
alert.show();
//例:
//代码:
//内容
TextView msg = new TextView(this);
msg.setText("确定清除本地所有客户数据?");
msg.setPadding(10, 10, 10, 10);
msg.setGravity(Gravity.CENTER);
msg.setTextSize(18);
//标题
TextView title = new TextView(this);
title.setText("警告");
title.setPadding(10, 10, 10, 10); //边距
title.setGravity(Gravity.CENTER); //位置
title.setTextColor(getResources().getColor(R.color.F212121));//字体的颜色
title.setTextSize(23); //字体的大小
new AlertDialog.Builder(this)
.setCustomTitle(title)//设置字体
.setView(msg)
.setNeutralButton("按钮1",null)
.setNegativeButton("按钮2", null)
.setPositiveButton("按钮3", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.create()
.show();
//---------------------------------------------------------------------完-----------------------------------------------------------------------------