/
AlertDialogUtil.java
123 lines (111 loc) · 3.56 KB
/
AlertDialogUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.liyi.sutils.utils;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
/**
* 系统弹出对话框工具类
*/
public class AlertDialogUtil {
/* 取消按钮的文字 确定按钮的文字 */
private String mNegBtnText, mPosBtnText;
/* 是否可以按返回键关闭 */
private boolean cancelable;
private Context mContext;
/* 取消按钮的点击监听 */
private OnAlertNegativeListener mNegativeListener;
/* 确定按钮的点击监听 */
private OnAlertPositiveListener mPositiveListener;
public static AlertDialogUtil newInstance(@NonNull Context context) {
return new AlertDialogUtil(context);
}
private AlertDialogUtil(@NonNull Context context) {
super();
this.mContext = context;
cancelable = false;
mNegBtnText = "取消";
mPosBtnText = "确定";
}
/**
* 显示对话框
*
* @param message 对话框的内容
*/
public void showDialog(String message) {
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setMessage(message)
.setCancelable(cancelable)
.setPositiveButton(mPosBtnText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (mPositiveListener != null) {
mPositiveListener.onPositiveClick(i);
}
}
})
.setNegativeButton(mNegBtnText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (mNegativeListener != null) {
mNegativeListener.onNegativeClick(i);
}
}
}).show();
}
/**
* 是否可以按返回键关闭
*
* @param cancelable {@code true}: 可以<br>{@code false}: 不可以
* @return AlertDialogUtil 类
*/
public AlertDialogUtil setCancelable(boolean cancelable) {
this.cancelable = cancelable;
return this;
}
/**
* 设置取消按钮的文字
*
* @param text 取消按钮的文字
* @return AlertDialogUtil 类
*/
public AlertDialogUtil negativeText(String text) {
this.mNegBtnText = text;
return this;
}
/**
* 设置确定按钮的文字
*
* @param text 确定按钮的文字
* @return AlertDialogUtil 类
*/
public AlertDialogUtil positiveText(String text) {
this.mPosBtnText = text;
return this;
}
/**
* 设置取消按钮的点击事件
*
* @param listener 取消按钮的点击事件监听
* @return AlertDialogUtil 类
*/
public AlertDialogUtil negative(OnAlertNegativeListener listener) {
this.mNegativeListener = listener;
return this;
}
/**
* 设置确定按钮的点击事件
*
* @param listener 确定按钮的点击事件监听
* @return AlertDialogUtil 类
*/
public AlertDialogUtil positive(OnAlertPositiveListener listener) {
this.mPositiveListener = listener;
return this;
}
public interface OnAlertNegativeListener {
void onNegativeClick(int i);
}
public interface OnAlertPositiveListener {
void onPositiveClick(int i);
}
}