|
3 | 3 | import android.app.Dialog; |
4 | 4 | import android.app.ProgressDialog; |
5 | 5 | import android.content.Context; |
| 6 | +import android.content.DialogInterface; |
| 7 | +import android.os.Handler; |
| 8 | +import android.support.v7.app.AlertDialog; |
6 | 9 | import android.widget.PopupWindow; |
7 | 10 |
|
8 | 11 | /** |
@@ -70,60 +73,223 @@ public static void closePopupWindows(PopupWindow... popupWindows){ |
70 | 73 | } |
71 | 74 | } |
72 | 75 |
|
73 | | - // == |
| 76 | + // = |
74 | 77 |
|
75 | 78 | /** |
76 | | - * 创建加载 Dialog |
| 79 | + * detail: Dialog 事件 |
| 80 | + * Created by Ttt |
| 81 | + */ |
| 82 | + public static abstract class DialogListener { |
| 83 | + |
| 84 | + /** |
| 85 | + * 最左边按钮点击事件 |
| 86 | + * @param dialog |
| 87 | + */ |
| 88 | + public void onLeftButton(DialogInterface dialog){ |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * 最右边按钮点击事件 |
| 93 | + * @param dialog |
| 94 | + */ |
| 95 | + public abstract void onRightButton(DialogInterface dialog); |
| 96 | + |
| 97 | + /** |
| 98 | + * 关闭通知 |
| 99 | + * @param dialog |
| 100 | + */ |
| 101 | + public void onDismiss(DialogInterface dialog){ |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * 创建提示 Dialog (原生样式) |
77 | 107 | * @param context |
78 | | - * @param title |
79 | | - * @param content |
| 108 | + * @param title dialog 标题 |
| 109 | + * @param content dialog 内容 |
| 110 | + * @param rightBtn 右边按钮文案 |
80 | 111 | * @return |
81 | 112 | */ |
82 | | - public static ProgressDialog creDialog(Context context, String title, String content){ |
83 | | - return creDialog(context, title, content, false);// 不可以使用返回键 |
| 113 | + public static AlertDialog createAlertDialog(Context context, String title, String content, String rightBtn){ |
| 114 | + return createAlertDialog(context, title, content, null, rightBtn, null); |
84 | 115 | } |
85 | 116 |
|
86 | 117 | /** |
87 | | - * 创建加载 Dialog |
| 118 | + * 创建提示 Dialog (原生样式) |
88 | 119 | * @param context |
89 | | - * @param title |
90 | | - * @param content |
91 | | - * @param isCancel |
| 120 | + * @param title dialog 标题 |
| 121 | + * @param content dialog 内容 |
| 122 | + * @param leftBtn 左边按钮文案 |
| 123 | + * @param rightBtn 右边按钮文案 |
92 | 124 | * @return |
93 | 125 | */ |
94 | | - public static ProgressDialog creDialog(Context context, String title, String content, boolean isCancel){ |
95 | | - ProgressDialog progressDialog = android.app.ProgressDialog.show(context, title, content); |
96 | | - progressDialog.setCancelable(isCancel); |
97 | | - return progressDialog; |
| 126 | + public static AlertDialog createAlertDialog(Context context, String title, String content, String leftBtn, String rightBtn){ |
| 127 | + return createAlertDialog(context, title, content, leftBtn, rightBtn, null); |
98 | 128 | } |
99 | 129 |
|
100 | 130 | /** |
101 | | - * 创建自动关闭dialog |
| 131 | + * 创建提示 Dialog (原生样式) |
102 | 132 | * @param context |
103 | | - * @param title |
104 | | - * @param content |
| 133 | + * @param title dialog 标题 |
| 134 | + * @param content dialog 内容 |
| 135 | + * @param rightBtn 右边按钮文案 |
| 136 | + * @param dialogListener 事件通知 |
105 | 137 | * @return |
106 | 138 | */ |
107 | | - public static ProgressDialog creAutoCloseDialog(Context context, String title, String content){ |
108 | | - final ProgressDialog progressDialog = android.app.ProgressDialog.show(context, title, content); |
109 | | - progressDialog.setCancelable(false); |
110 | | - Thread thread = new Thread(new Runnable() { |
111 | | - @Override |
112 | | - public void run() { |
113 | | - try { |
114 | | - // 显示10秒后,取消 ProgressDialog |
115 | | - Thread.sleep(10000); |
116 | | - } catch (InterruptedException e) { |
117 | | - } |
118 | | - try { |
119 | | - if (progressDialog != null) { |
120 | | - progressDialog.dismiss(); |
| 139 | + public static AlertDialog createAlertDialog(Context context, String title, String content, String rightBtn, DialogListener dialogListener){ |
| 140 | + return createAlertDialog(context, title, content, null, rightBtn, dialogListener); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * 创建提示 Dialog (原生样式) |
| 145 | + * @param context |
| 146 | + * @param title dialog 标题 |
| 147 | + * @param content dialog 内容 |
| 148 | + * @param leftBtn 左边按钮文案 |
| 149 | + * @param rightBtn 右边按钮文案 |
| 150 | + * @param dialogListener 事件通知 |
| 151 | + * @return |
| 152 | + */ |
| 153 | + public static AlertDialog createAlertDialog(Context context, String title, String content, String leftBtn, String rightBtn, final DialogListener dialogListener){ |
| 154 | + AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 155 | + builder.setTitle(title); |
| 156 | + builder.setMessage(content); |
| 157 | + |
| 158 | + if (leftBtn != null){ |
| 159 | + if (dialogListener == null) { |
| 160 | + builder.setNegativeButton(leftBtn, null); |
| 161 | + } else { |
| 162 | + builder.setNegativeButton(leftBtn, new DialogInterface.OnClickListener() { |
| 163 | + @Override |
| 164 | + public void onClick(DialogInterface dialog, int which) { |
| 165 | + if (dialogListener != null) { |
| 166 | + dialogListener.onLeftButton(dialog); |
| 167 | + } |
121 | 168 | } |
122 | | - } catch (Exception e) { |
123 | | - } |
| 169 | + }); |
124 | 170 | } |
125 | | - }); |
126 | | - thread.start(); |
| 171 | + } |
| 172 | + |
| 173 | + if (rightBtn != null){ |
| 174 | + if (dialogListener == null) { |
| 175 | + builder.setPositiveButton(rightBtn, null); |
| 176 | + } else { |
| 177 | + builder.setPositiveButton(rightBtn, new DialogInterface.OnClickListener() { |
| 178 | + @Override |
| 179 | + public void onClick(DialogInterface dialog, int which) { |
| 180 | + if (dialogListener != null) { |
| 181 | + dialogListener.onRightButton(dialog); |
| 182 | + } |
| 183 | + } |
| 184 | + }); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + if (dialogListener != null){ |
| 189 | + builder.setOnDismissListener(new DialogInterface.OnDismissListener() { |
| 190 | + @Override |
| 191 | + public void onDismiss(DialogInterface dialog) { |
| 192 | + if (dialogListener != null) { |
| 193 | + dialogListener.onDismiss(dialog); |
| 194 | + } |
| 195 | + } |
| 196 | + }); |
| 197 | + } |
| 198 | + return builder.create(); |
| 199 | + } |
| 200 | + |
| 201 | + // = |
| 202 | + |
| 203 | + /** |
| 204 | + * 创建加载中 Dialog (原生样式) |
| 205 | + * @param context |
| 206 | + * @param title dialog 标题 |
| 207 | + * @param content dialog 内容 |
| 208 | + * @return |
| 209 | + */ |
| 210 | + public static ProgressDialog createProgressDialog(Context context, String title, String content){ |
| 211 | + return createProgressDialog(context, title, content, false, null); |
| 212 | + } |
| 213 | + |
| 214 | + /** |
| 215 | + * 创建加载中 Dialog (原生样式) |
| 216 | + * @param context |
| 217 | + * @param title dialog 标题 |
| 218 | + * @param content dialog 内容 |
| 219 | + * @param isCancel 是否可以返回键关闭 |
| 220 | + * @return |
| 221 | + */ |
| 222 | + public static ProgressDialog createProgressDialog(Context context, String title, String content, boolean isCancel){ |
| 223 | + return createProgressDialog(context, title, content, isCancel, null); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * 创建加载中 Dialog (原生样式) |
| 228 | + * @param context |
| 229 | + * @param title dialog 标题 |
| 230 | + * @param content dialog 内容 |
| 231 | + * @param isCancel 是否可以返回键关闭 |
| 232 | + * @return |
| 233 | + */ |
| 234 | + public static ProgressDialog createProgressDialog(Context context, String title, String content, boolean isCancel, DialogInterface.OnCancelListener cancelListener){ |
| 235 | + ProgressDialog progressDialog = android.app.ProgressDialog.show(context, title, content, isCancel); |
| 236 | + progressDialog.setOnCancelListener(cancelListener); |
127 | 237 | return progressDialog; |
128 | 238 | } |
| 239 | + |
| 240 | + // = |
| 241 | + |
| 242 | + /** |
| 243 | + * 自动关闭dialog |
| 244 | + * @param dialog |
| 245 | + * @param time |
| 246 | + * @param handler |
| 247 | + * @return |
| 248 | + */ |
| 249 | + public static <T extends Dialog> T autoCloseDialog(final T dialog, final long time, Handler handler){ |
| 250 | + // 不为null, 并且显示中 |
| 251 | + if (dialog != null && dialog.isShowing()){ |
| 252 | + if (handler != null){ |
| 253 | + handler.postDelayed(new Runnable() { |
| 254 | + @Override |
| 255 | + public void run() { |
| 256 | + try { |
| 257 | + if (dialog != null && dialog.isShowing()){ |
| 258 | + dialog.dismiss(); |
| 259 | + } |
| 260 | + } catch (Exception e){ |
| 261 | + } |
| 262 | + } |
| 263 | + }, time); |
| 264 | + } |
| 265 | + } |
| 266 | + return dialog; |
| 267 | + } |
| 268 | + |
| 269 | + /** |
| 270 | + * 自动关闭 PopupWindow |
| 271 | + * @param popupWindow |
| 272 | + * @param time |
| 273 | + * @param handler |
| 274 | + * @return |
| 275 | + */ |
| 276 | + public static <T extends PopupWindow> T autoClosePopupWindow(final T popupWindow, final long time, Handler handler){ |
| 277 | + // 不为null, 并且显示中 |
| 278 | + if (popupWindow != null && popupWindow.isShowing()){ |
| 279 | + if (handler != null){ |
| 280 | + handler.postDelayed(new Runnable() { |
| 281 | + @Override |
| 282 | + public void run() { |
| 283 | + try { |
| 284 | + if (popupWindow != null && popupWindow.isShowing()){ |
| 285 | + popupWindow.dismiss(); |
| 286 | + } |
| 287 | + } catch (Exception e){ |
| 288 | + } |
| 289 | + } |
| 290 | + }, time); |
| 291 | + } |
| 292 | + } |
| 293 | + return popupWindow; |
| 294 | + } |
129 | 295 | } |
0 commit comments