public class DialogUtil {
//使用弱引用防止内存泄漏
private static WeakReference ref_alert;
private static WeakReference ref_progress;
public static void alertDialog(Context context, String title, String message) {
alertDialog(context,null,null,title,message,true,null);
}
public static void alertDialog(Context context, String title, String message,boolean isCancel, final Runnable taskPositive) {
alertDialog(context,"ok","cancel",title,message,isCancel,taskPositive);
}
public static void dismiss(){
if (ref_progress.get() !=null&& ref_progress.get().isShowing()){
ref_progress.get().dismiss();
}
if (ref_alert.get()!=null&&ref_alert.get().isShowing()){
ref_alert.get().dismiss();
}
}
public static void alertDialog(Context context, String positiveText,String negativeText,String title, String message,boolean isCancel, final Runnable taskPositive) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (!TextUtils.isEmpty(title))
builder.setTitle(title);
if (!TextUtils.isEmpty(title))
builder.setMessage(message);
builder.setCancelable(isCancel);
if (!TextUtils.isEmpty(positiveText)) {
builder.setPositiveButton(positiveText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (taskPositive != null) {
taskPositive.run();
}
dialog.dismiss();
}
});
}
if (!TextUtils.isEmpty(negativeText)) {
builder.setNegativeButton(negativeText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
});
}
AlertDialog alertDialog = builder.create();
ref_alert = new WeakReference(alertDialog);
alertDialog.show();
}
// public static void loading(Context context,boolean isCancel) {
//
// ProgressBar progressBar = new ProgressBar(context);
// dialog = new Dialog(context);
// dialog.requestWindowFeature(1);
// dialog.getWindow().setBackgroundDrawableResource(R.drawable.transparent_bg);
// Window window = dialog.getWindow();
// window.setAttributes( window.getAttributes());
// dialog.setContentView(progressBar);
// dialog.show();
// }
public static void customView(Context context, View view, Runnable runTask) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(view);
if (runTask!=null){
runTask.run();
}
AlertDialog alertDialog = builder.create();
ref_alert = new WeakReference(alertDialog);
alertDialog.show();
}
public static void customView(Context context, @LayoutRes int view, Runnable runTask) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(view);
if (runTask!=null){
runTask.run();
}
AlertDialog alertDialog = builder.create();
ref_alert = new WeakReference(alertDialog);
alertDialog.show();
}
/**
* 圆形进度转圈
*
* @param context
* @param title 标题 这个可为null
* @param message 内容
* @param cancelable 点击外部是否消失 一般为true可以点击消失
* @param cancelListener dialog取消监听器
*/
public static void showLoadingDialog(Context context, String title, String message, Boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
ProgressDialog progressDialog = ProgressDialog.show(context, title, message, false,
cancelable, cancelListener);
ref_progress = new WeakReference<>(progressDialog);
}
public static void showLoadingDialog(Context context, String title, String message, Boolean cancelable) {
ProgressDialog progressDialog = ProgressDialog.show(context, title, message, false,
cancelable);
ref_progress = new WeakReference<>(progressDialog);
}
/**
* 水平进度条
*
* @param context
* @param title
* @param message
* @param cancelable
*/
public static void showHorizhontalLoadingDialog(Context context, String title, String message, Boolean cancelable) {
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平进度条
progressDialog.setCancelable(true);// 设置是否可以通过点击Back键取消
progressDialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
progressDialog.setIcon(null);// 设置提示的title的图标,默认是没有的
progressDialog.setTitle(title);
progressDialog.setMessage(message);
progressDialog.setMax(100);
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
progressDialog.show();
ref_progress = new WeakReference<>(progressDialog);
// AppOperator.runOnThread(new Runnable() {
//
// @Override
// public void run() {
// int i = 0;
// while (i < 100) {
// try {
// Thread.sleep(200);
// // 更新进度条的进度,可以在子线程中更新进度条进度
// progressDialog.incrementProgressBy(1);
// // progressDialog.incrementSecondaryProgressBy(10)//二级进度条更新方式
// i++;
// } catch (Exception e) {
// }
// }
// // 在进度条走完时删除Dialog
// progressDialog.dismiss();
// }
// });
}
}