android简单自封DialogUtil

public class DialogUtil {
	//使用弱引用防止内存泄漏
    private static WeakReference<AlertDialog> ref_alert;
    private static WeakReference<ProgressDialog> 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);
        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);
        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);
        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();
//            }
//        });
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值