Android DialogUtils

本文介绍了一个基于SweetAlertDialog封装的AlterDialog工具类,该工具类提供了一种简单的方法来展示不同类型的对话框,如进度提示、成功提示、警告提示等,并且可以在Android应用中轻松集成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AlterDialog 工具类

对SweetAlertDialog 进行封装

  • 引入第三方模块
  • 工具实现
    AS 引入第三方对话框插件
implementation 'cn.pedant.sweetalert:library:1.3'

代码实现

public class DialogUtils {

    private static SweetAlertDialog sweetAlertDialog = null;
    /**
     * 显示进度条
     * @param context
     * @return
     */
    public static SweetAlertDialog showProgress(Context context){
        try {
            if(context!=null){
                if(sweetAlertDialog==null){
                    synchronized (SweetAlertDialog.class) {
                        sweetAlertDialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE);
                    }
                }
                sweetAlertDialog.setTitleText("请稍后...");
                sweetAlertDialog.setCancelable(true);
                sweetAlertDialog.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog mSweetAlertDialog) {
                        mSweetAlertDialog.dismiss();
                        sweetAlertDialog = null;
                    }
                });
                sweetAlertDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sweetAlertDialog;
    }

    /**
     * 成功提示
     * @param context
     * @param msg
     * @param listener
     * @return
     */
    public static SweetAlertDialog showSucceed(Context context, String msg, SweetAlertDialog.OnSweetClickListener listener){
        try {
            if(context!=null){
                if(sweetAlertDialog==null){
                    synchronized (SweetAlertDialog.class) {
                        sweetAlertDialog = new SweetAlertDialog(context, SweetAlertDialog.SUCCESS_TYPE);
                    }
                }
                sweetAlertDialog.setTitleText("提示");
                sweetAlertDialog.setContentText(msg);
                sweetAlertDialog.setCancelText("取消");
                sweetAlertDialog.setConfirmText("确认");
                sweetAlertDialog.setConfirmClickListener(listener);
                sweetAlertDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sweetAlertDialog;
    }

    /**
     * 警告提示
     * @param context
     * @param msg
     * @param listener
     * @return
     */
    public static SweetAlertDialog showWarning(Context context, String msg, SweetAlertDialog.OnSweetClickListener listener){
        try {
            if(context!=null){
                if(sweetAlertDialog==null){
                    synchronized (SweetAlertDialog.class) {
                        sweetAlertDialog = new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE);
                    }
                }
                sweetAlertDialog.setTitleText("提示");
                sweetAlertDialog.setContentText(msg);
                sweetAlertDialog.setCancelText("取消");
                sweetAlertDialog.setConfirmText("确认");
                sweetAlertDialog.setConfirmClickListener(listener);
                sweetAlertDialog.show();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return sweetAlertDialog;
    }

    public static SweetAlertDialog showError(Context context, String msg, SweetAlertDialog.OnSweetClickListener listener){

        try {
            if(context!=null){
                if(sweetAlertDialog==null){
                    synchronized (SweetAlertDialog.class) {
                        sweetAlertDialog = new SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE);
                    }
                }
                sweetAlertDialog.setTitleText("提示");
                sweetAlertDialog.setContentText(msg);
                sweetAlertDialog.setCancelText("取消");
                sweetAlertDialog.setConfirmText("确认");
                sweetAlertDialog.setConfirmClickListener(listener);
                sweetAlertDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sweetAlertDialog;
    }
    /**
     *
     * @param context
     * @return
     */
    public static SweetAlertDialog showProgress(Context context, String msg){
        try {
            if(context!=null){
                if(sweetAlertDialog==null){
                    synchronized (SweetAlertDialog.class) {
                        sweetAlertDialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE);
                    }
                }
                sweetAlertDialog.setTitleText(msg);
                sweetAlertDialog.setCancelable(true);
                sweetAlertDialog.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog mSweetAlertDialog) {
                        mSweetAlertDialog.dismiss();
                        sweetAlertDialog = null;
                    }
                });
                sweetAlertDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sweetAlertDialog;
    }

    public static void dismissDialog(){
        if(sweetAlertDialog!=null){
            try {
                sweetAlertDialog.dismiss();
                sweetAlertDialog = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     *
     * @param context
     * @return
     */
    public static SweetAlertDialog showSucceed(Context context, String msg){
        try {
            if (context != null) {
                if(sweetAlertDialog==null){
                    synchronized (SweetAlertDialog.class) {
                        sweetAlertDialog = new SweetAlertDialog(context, SweetAlertDialog.SUCCESS_TYPE);
                    }
                }
                sweetAlertDialog.setContentText(msg);
                sweetAlertDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sweetAlertDialog;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值