给 SweetAlertDialog 设置监听器

SweetAlertDialog这个库很好用,是关于对话框的第三方库。

package com.baozhuwanglei.android.xiaofanghuiluandroidbzw.xiaofanghuiluandroidbzw.common;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baozhuwanglei.android.xiaofanghuiluandroidbzw.xiaofanghuiluandroidbzw.R;
import com.baozhuwanglei.android.xiaofanghuiluandroidbzw.xiaofanghuiluandroidbzw.api.Api;
import com.baozhuwanglei.android.xiaofanghuiluandroidbzw.xiaofanghuiluandroidbzw.service.DownloadService;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by baozhuwanglei on 17-4-13.
 */
public class DialogManager {
    private static DialogManager instance;
    private SweetAlertDialog loadDialog;

    private DialogManager() {}

    public static DialogManager getInstance() {
        if (instance == null) {
            instance = new DialogManager();
        }
        return instance;
    }

    /**
     * @param context 上下文
     * @param title   标题
     *                

* 加载中 */ public void loadDialog(Context context, String title) { loadDialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE); loadDialog.getProgressHelper().setBarColor(ContextCompat.getColor(context, R.color.colorPrimaryDark)); loadDialog.setTitleText(title); loadDialog.setCancelable(false); loadDialog.show(); } public void loadDialogDismiss() { if (loadDialog.isShowing()) { loadDialog.dismiss(); } } /** * 显示消息 * @param context 上下文 * @param title 标题 * @param content 内容 * @param type 类型 */ public void showMsgDialog(Context context, String title, String content, int type) { SweetAlertDialog msgDialog = new SweetAlertDialog(context, type); msgDialog.setTitleText(title); msgDialog.setContentText(content); msgDialog.show(); } /** * 重载 加入监听器 */ public void showMsgDialog(Context context, String title, String content, int type, SweetAlertDialog.OnSweetClickListener listener) { SweetAlertDialog msgDialog = new SweetAlertDialog(context, type); msgDialog.setTitleText(title); msgDialog.setContentText(content); msgDialog.setConfirmText("确定"); msgDialog.show(); msgDialog.setConfirmClickListener(listener); } /** * 网络超时 */ public void errorDialog(Context context) { SweetAlertDialog msgDialog = new SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE); msgDialog.setTitleText("提示"); msgDialog.getProgressHelper().setBarColor(R.color.colorPrimary); msgDialog.setContentText("网络超时,请重试"); msgDialog.setConfirmText("确定"); msgDialog.show(); msgDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() { @Override public void onClick(SweetAlertDialog sweetAlertDialog) { sweetAlertDialog.dismiss(); } }); } /** * 警告 * confirm */ public void showWarmDialog(Context context, String title, String content, String confirmText, SweetAlertDialog.OnSweetClickListener listener) { SweetAlertDialog dialog = new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE); dialog.setTitleText(title); dialog.setContentText(content); dialog.setConfirmText(confirmText); dialog.setCancelText("取消"); dialog.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() { @Override public void onClick(SweetAlertDialog sweetAlertDialog) { sweetAlertDialog.dismiss(); } }); dialog.setConfirmClickListener(listener); dialog.show(); } /** * 版本更新 dialog */ public void showUpdateDialog(final Context context, final Activity activity, String titleString, String contentString) { final Dialog dialog = new Dialog(context, R.style.dialog); dialog.setCancelable(false); dialog.setContentView(R.layout.update_app_dialog); TextView mTitleTv = (TextView) dialog.findViewById(R.id.c_title_tv); TextView contentTv = (TextView) dialog.findViewById(R.id.update_content_tv); Button mNegativeButton = (Button) dialog.findViewById(R.id.negative_btn); Button mPositiveButton = (Button) dialog.findViewById(R.id.positive_btn); mTitleTv.setText(titleString); contentTv.setText(contentString); mNegativeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); mPositiveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(activity, DownloadService.class); intent.putExtra("downUrl", Api.VERSION_UPDATE); activity.startService(intent); dialog.dismiss(); Toast.makeText(context, "后台加载中...", Toast.LENGTH_SHORT).show(); } }); dialog.show(); } }

这个类封装了 SweetAlertDialog,这个类不是我写的,但是我加入了一个重载方法,这样就可以让showMsgDialog方法能够去设置监听器了。实现监听器类很简单:

new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
            }
        });

发表评论

电子邮件地址不会被公开。 必填项已用*标注