/qq137722697/article/details/53295856
/p/fb671e11e455
使用链式构建一个正常的dialog
MDialog.build().with(getActivity()).title("警告").message("您要删除吗?").show(new MDialog.OnClickCallback() {@Overridepublic void onOk() {Toast.makeText(getActivity(), "OnClickCallback", Toast.LENGTH_SHORT).show();}});
改变颜色、字体大小、图标设置、添加取消响应事件
MDialog.build().with(getActivity()).setIconResId(R.mipmap.dialogicon).title("警告").setTitleSize(24).setTitleColor("#ff0000").message("您确定要删除吗?").setMessageSize(23).setMsgColor("#BCD1D4").setOK("更新").setOKSize(25).setOkColor("#663123").setCancel("跳过").setCancelSize(22).setCancelColor("#000000").show(new MDialog.OnClickCallback() {@Overridepublic void onOk() {Toast.makeText(getActivity(), "OnClickCallback", Toast.LENGTH_SHORT).show();}@Overridepublic void onCancel() {Toast.makeText(getActivity(), "onCancel", Toast.LENGTH_SHORT).show();}});
自定义MDialog
/*** 作者:Created by zhou on /7/3 08:51* 邮箱:qishuichixi@* 版本:V 1.0* 描述:*/public class MDialog {private static MDialog mDialog;private String title;//标题private String message;//提示内容private int iconResId;//图标private Context context;//上下文对象private String OK;//确认private String Cancel;//取消private int btnOKSize;private int btnCancelSize;private int titleSize;//标题大小private int messageSize;//主题大小private String titleColor;//标题颜色private String msgColor;//信息颜色private String okColor;//颜色private String cancelColor;//标题颜色private MDialog() {}public static MDialog build() {// if (mDialog == null) {// synchronized (MDialog.class) {//if (mDialog == null) {//mDialog = new MDialog();//}// }// }// return mDialog;return new MDialog();}//设置上下文对象public MDialog with(Context context) {this.context = context;return this;}// 设置标题public MDialog title(String title) {this.title = title;return this;}//设置消息public MDialog message(String message) {this.message = message;return this;}//设置图标public MDialog setIconResId(int iconResId) {this.iconResId = iconResId;return this;}//设置确认public MDialog setOK(String OK) {this.OK = OK;return this;}public MDialog setCancel(String Cancel) {this.Cancel = Cancel;return this;}public MDialog setTitleSize(int titleSize) {this.titleSize = titleSize;return this;}//设置颜色public MDialog setMessageSize(int messageSize) {this.messageSize = messageSize;return this;}public MDialog setOKSize(int btnOKSize) {this.btnOKSize = btnOKSize;return this;}public MDialog setCancelSize(int btnCancelSize) {this.btnCancelSize = btnCancelSize;return this;}public MDialog setTitleColor(String titleColor) {this.titleColor = titleColor;return this;}public MDialog setMsgColor(String msgColor) {this.msgColor = msgColor;return this;}public MDialog setOkColor(String okColor) {this.okColor = okColor;return this;}public MDialog setCancelColor(String cancelColor) {this.cancelColor = cancelColor;return this;}//显示对话框public void show(final OnClickCallback callback) {AlertDialog.Builder builder = new AlertDialog.Builder(context);if (!TextUtils.isEmpty(title)) {builder.setTitle(title);}if (!TextUtils.isEmpty(message)) {builder.setMessage(message);}if (iconResId != 0) {builder.setIcon(iconResId);}if (TextUtils.isEmpty(Cancel)) {Cancel = "取消";}if (TextUtils.isEmpty(OK)) {OK = "确认";}builder.setNegativeButton(Cancel, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {callback.onCancel();}});builder.setPositiveButton(OK, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {callback.onOk();}});AlertDialog dialog = builder.create();dialog.show();//设置属性setAttribute(dialog);}private void setAttribute(AlertDialog dialog) {//设置标题DialogTitle title = (DialogTitle) dialog.findViewById(R.id.alertTitle);TextView textView = (TextView) dialog.findViewById(android.R.id.message);Button positiveBtn = dialog.getButton(DialogInterface.BUTTON_POSITIVE);Button negativeBtn = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);//设置大小if (titleSize != 0) title.setTextSize(titleSize);if (messageSize != 0) textView.setTextSize(messageSize);if (btnOKSize != 0) positiveBtn.setTextSize(btnOKSize);if (btnCancelSize != 0) negativeBtn.setTextSize(btnCancelSize);//设置颜色try {if(!TextUtils.isEmpty(okColor))positiveBtn.setTextColor(Color.parseColor(okColor));if(!TextUtils.isEmpty(cancelColor))negativeBtn.setTextColor(Color.parseColor(cancelColor));if(!TextUtils.isEmpty(titleColor))title.setTextColor(Color.parseColor(titleColor));if(!TextUtils.isEmpty(msgColor))textView.setTextColor(Color.parseColor(msgColor));}catch (Exception e){throw new RuntimeException("颜色属性设置出错,格式为#123456");}}interface IOnClickCallback {void onOk();//点击确定的时候回调}public static abstract class OnClickCallback implements IOnClickCallback {public void onCancel() {//点击取消的时候回调}}}
/s/1nvBhkkL