|
8 | 8 | import android.support.annotation.LayoutRes; |
9 | 9 | import android.support.annotation.NonNull; |
10 | 10 | import android.support.annotation.StringRes; |
| 11 | +import android.support.v4.widget.TextViewCompat; |
11 | 12 | import android.text.SpannableString; |
12 | 13 | import android.text.Spanned; |
13 | 14 | import android.text.style.ForegroundColorSpan; |
| 15 | +import android.util.Log; |
14 | 16 | import android.view.Gravity; |
15 | 17 | import android.view.LayoutInflater; |
16 | 18 | import android.view.View; |
| 19 | +import android.widget.TextView; |
17 | 20 | import android.widget.Toast; |
18 | 21 |
|
19 | 22 | import java.lang.ref.WeakReference; |
|
28 | 31 | */ |
29 | 32 | public final class ToastUtils { |
30 | 33 |
|
| 34 | + private static final String TAG = "ToastUtils"; |
31 | 35 | private static final int DEFAULT_COLOR = 0x12000000; |
32 | 36 | private static final Handler sHandler = new Handler(Looper.getMainLooper()); |
33 | | - private static Toast sToast; |
34 | | - private static WeakReference<View> sViewWeakReference; |
| 37 | + private static WeakReference<Toast> sToastWeakReference; |
| 38 | + private static WeakReference<View> sViewWeakReference; |
35 | 39 | private static int gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; |
36 | 40 | private static int xOffset = 0; |
37 | 41 | private static int yOffset = (int) (64 * Utils.getApp().getResources().getDisplayMetrics().density + 0.5); |
@@ -81,13 +85,14 @@ public static void setView(final View view) { |
81 | 85 | * @return view |
82 | 86 | */ |
83 | 87 | public static View getView() { |
84 | | - if (sViewWeakReference != null) { |
85 | | - final View view = sViewWeakReference.get(); |
86 | | - if (view != null) { |
87 | | - return view; |
88 | | - } |
| 88 | + final View view = getViewFromWR(); |
| 89 | + if (view != null) { |
| 90 | + return view; |
| 91 | + } |
| 92 | + final Toast toast = getToastFromWR(); |
| 93 | + if (toast != null) { |
| 94 | + return toast.getView(); |
89 | 95 | } |
90 | | - if (sToast != null) return sToast.getView(); |
91 | 96 | return null; |
92 | 97 | } |
93 | 98 |
|
@@ -434,43 +439,65 @@ private static void show(final String format, final int duration, final Object.. |
434 | 439 | */ |
435 | 440 | private static void show(final CharSequence text, final int duration) { |
436 | 441 | cancel(); |
437 | | - boolean isCustom = false; |
438 | | - if (sViewWeakReference != null) { |
439 | | - final View view = sViewWeakReference.get(); |
440 | | - if (view != null) { |
441 | | - sToast = new Toast(Utils.getApp()); |
442 | | - sToast.setView(view); |
443 | | - sToast.setDuration(duration); |
444 | | - isCustom = true; |
445 | | - } |
446 | | - } |
447 | | - if (!isCustom) { |
| 442 | + Toast toast; |
| 443 | + final View view = getViewFromWR(); |
| 444 | + if (view != null) { |
| 445 | + toast = new Toast(Utils.getApp()); |
| 446 | + toast.setView(view); |
| 447 | + toast.setDuration(duration); |
| 448 | + } else { |
448 | 449 | if (messageColor != DEFAULT_COLOR) { |
449 | 450 | SpannableString spannableString = new SpannableString(text); |
450 | 451 | ForegroundColorSpan colorSpan = new ForegroundColorSpan(messageColor); |
451 | 452 | spannableString.setSpan(colorSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
452 | | - sToast = Toast.makeText(Utils.getApp(), spannableString, duration); |
| 453 | + toast = Toast.makeText(Utils.getApp(), spannableString, duration); |
453 | 454 | } else { |
454 | | - sToast = Toast.makeText(Utils.getApp(), text, duration); |
| 455 | + toast = Toast.makeText(Utils.getApp(), text, duration); |
455 | 456 | } |
| 457 | + // solve the font of toast |
| 458 | + TextViewCompat.setTextAppearance((TextView) toast.getView().findViewById(android.R.id.message), android.R.style.TextAppearance); |
456 | 459 | } |
457 | | - View view = sToast.getView(); |
| 460 | + View toastView = toast.getView(); |
458 | 461 | if (bgResource != -1) { |
459 | | - view.setBackgroundResource(bgResource); |
| 462 | + toastView.setBackgroundResource(bgResource); |
460 | 463 | } else if (backgroundColor != DEFAULT_COLOR) { |
461 | | - view.setBackgroundColor(backgroundColor); |
| 464 | + toastView.setBackgroundColor(backgroundColor); |
462 | 465 | } |
463 | | - sToast.setGravity(gravity, xOffset, yOffset); |
464 | | - sToast.show(); |
| 466 | + toast.setGravity(gravity, xOffset, yOffset); |
| 467 | + sToastWeakReference = new WeakReference<>(toast); |
| 468 | + toast.show(); |
465 | 469 | } |
466 | 470 |
|
467 | 471 | /** |
468 | 472 | * 取消吐司显示 |
469 | 473 | */ |
470 | 474 | public static void cancel() { |
471 | | - if (sToast != null) { |
472 | | - sToast.cancel(); |
473 | | - sToast = null; |
| 475 | + Toast toast = getToastFromWR(); |
| 476 | + if (toast != null) { |
| 477 | + toast.cancel(); |
474 | 478 | } |
| 479 | + sToastWeakReference = null; |
| 480 | + } |
| 481 | + |
| 482 | + private static Toast getToastFromWR() { |
| 483 | + if (sToastWeakReference != null) { |
| 484 | + final Toast toast = sToastWeakReference.get(); |
| 485 | + if (toast != null) { |
| 486 | + return toast; |
| 487 | + } |
| 488 | + } |
| 489 | + Log.e(TAG, "getToastFromWR: ", new NullPointerException("Toast is null")); |
| 490 | + return null; |
| 491 | + } |
| 492 | + |
| 493 | + private static View getViewFromWR() { |
| 494 | + if (sViewWeakReference != null) { |
| 495 | + final View view = sViewWeakReference.get(); |
| 496 | + if (view != null) { |
| 497 | + return view; |
| 498 | + } |
| 499 | + } |
| 500 | + Log.e(TAG, "getViewFromWR: ", new NullPointerException("The custom view of toast is null")); |
| 501 | + return null; |
475 | 502 | } |
476 | 503 | } |
0 commit comments