20
20
import android .widget .TextView ;
21
21
import android .widget .Toast ;
22
22
23
+ import java .lang .ref .WeakReference ;
24
+
23
25
/**
24
26
* <pre>
25
27
* author: Blankj
30
32
*/
31
33
public final class ToastUtils {
32
34
33
- private static final int COLOR_DEFAULT = 0xFEFFFFFF ;
34
- private static final Handler HANDLER = new Handler (Looper .getMainLooper ());
35
+ private static final int COLOR_DEFAULT = 0xFEFFFFFF ;
36
+ private static final Handler HANDLER = new Handler (Looper .getMainLooper ());
35
37
36
- private static Toast sToast ;
37
- private static int sGravity = -1 ;
38
- private static int sXOffset = -1 ;
39
- private static int sYOffset = -1 ;
40
- private static int sBgColor = COLOR_DEFAULT ;
38
+ private static WeakReference < Toast > sToast ;
39
+ private static int sGravity = -1 ;
40
+ private static int sXOffset = -1 ;
41
+ private static int sYOffset = -1 ;
42
+ private static int sBgColor = COLOR_DEFAULT ;
41
43
private static int sBgResource = -1 ;
42
- private static int sMsgColor = COLOR_DEFAULT ;
44
+ private static int sMsgColor = COLOR_DEFAULT ;
45
+ private static int sMsgTextSize = -1 ;
43
46
44
47
private ToastUtils () {
45
48
throw new UnsupportedOperationException ("u can't instantiate me..." );
@@ -85,6 +88,15 @@ public static void setMsgColor(@ColorInt final int msgColor) {
85
88
sMsgColor = msgColor ;
86
89
}
87
90
91
+ /**
92
+ * Set the text size of message.
93
+ *
94
+ * @param textSize The text size of message.
95
+ */
96
+ public static void setMsgTextSize (final int textSize ) {
97
+ sMsgTextSize = textSize ;
98
+ }
99
+
88
100
/**
89
101
* Show the toast for a short period of time.
90
102
*
@@ -199,8 +211,9 @@ public static View showCustomLong(@LayoutRes final int layoutId) {
199
211
* Cancel the toast.
200
212
*/
201
213
public static void cancel () {
202
- if (sToast != null ) {
203
- sToast .cancel ();
214
+ Toast toast ;
215
+ if (sToast != null && (toast = sToast .get ()) != null ) {
216
+ toast .cancel ();
204
217
sToast = null ;
205
218
}
206
219
}
@@ -222,8 +235,9 @@ private static void show(final CharSequence text, final int duration) {
222
235
@ Override
223
236
public void run () {
224
237
cancel ();
225
- sToast = Toast .makeText (Utils .getTopActivityOrApp (), text , duration );
226
- final TextView tvMessage = sToast .getView ().findViewById (android .R .id .message );
238
+ Toast toast = Toast .makeText (Utils .getTopActivityOrApp (), text , duration );
239
+ sToast = new WeakReference <>(toast );
240
+ final TextView tvMessage = toast .getView ().findViewById (android .R .id .message );
227
241
int msgColor = tvMessage .getCurrentTextColor ();
228
242
//it solve the font of toast
229
243
TextViewCompat .setTextAppearance (tvMessage , android .R .style .TextAppearance );
@@ -232,11 +246,14 @@ public void run() {
232
246
} else {
233
247
tvMessage .setTextColor (msgColor );
234
248
}
249
+ if (sMsgTextSize != -1 ) {
250
+ tvMessage .setTextSize (sMsgTextSize );
251
+ }
235
252
if (sGravity != -1 || sXOffset != -1 || sYOffset != -1 ) {
236
- sToast .setGravity (sGravity , sXOffset , sYOffset );
253
+ toast .setGravity (sGravity , sXOffset , sYOffset );
237
254
}
238
- setBg (tvMessage );
239
- sToast .show ();
255
+ setBg (toast , tvMessage );
256
+ toast .show ();
240
257
}
241
258
});
242
259
}
@@ -246,20 +263,22 @@ private static void show(final View view, final int duration) {
246
263
@ Override
247
264
public void run () {
248
265
cancel ();
249
- sToast = new Toast (Utils .getTopActivityOrApp ());
250
- sToast .setView (view );
251
- sToast .setDuration (duration );
266
+ Toast toast = new Toast (Utils .getTopActivityOrApp ());
267
+ sToast = new WeakReference <>(toast );
268
+
269
+ toast .setView (view );
270
+ toast .setDuration (duration );
252
271
if (sGravity != -1 || sXOffset != -1 || sYOffset != -1 ) {
253
- sToast .setGravity (sGravity , sXOffset , sYOffset );
272
+ toast .setGravity (sGravity , sXOffset , sYOffset );
254
273
}
255
- setBg ();
256
- sToast .show ();
274
+ setBg (toast );
275
+ toast .show ();
257
276
}
258
277
});
259
278
}
260
279
261
- private static void setBg () {
262
- View toastView = sToast .getView ();
280
+ private static void setBg (Toast toast ) {
281
+ View toastView = toast .getView ();
263
282
if (sBgResource != -1 ) {
264
283
toastView .setBackgroundResource (sBgResource );
265
284
} else if (sBgColor != COLOR_DEFAULT ) {
@@ -274,8 +293,8 @@ private static void setBg() {
274
293
}
275
294
}
276
295
277
- private static void setBg (final TextView tvMsg ) {
278
- View toastView = sToast .getView ();
296
+ private static void setBg (final Toast toast , final TextView tvMsg ) {
297
+ View toastView = toast .getView ();
279
298
if (sBgResource != -1 ) {
280
299
toastView .setBackgroundResource (sBgResource );
281
300
tvMsg .setBackgroundColor (Color .TRANSPARENT );
0 commit comments