package com.devin.util;

import android.support.annotation.StringRes;
import android.view.Gravity;
import android.widget.Toast;

import com.devin.UtilManager;

/**
 * <p>Description: 关于 Toast 的工具类
 * <p>Company：
 * <p>Email:bjxm2013@163.com
 * <p>Created by Devin Sun on 2017/4/26.
 */
public class ToastUtils {

    private static Toast toast;


    /**
     * Make a standard toast that just contains a text view with the text from a resource.
     * @param resId The resource id of the string resource to use.  Can be formatted tex
     */
    public static void showCenter(@StringRes int resId) {
        showCenter(UtilManager.getContext().getResources().getString(resId));
    }

    /**
     * Make a standard toast that just contains a text view.
     *
     * @param text Toast str
     */
    public static void showCenter(String text) {
        if (toast == null) {
            toast = Toast.makeText(UtilManager.getContext(), "", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.setText(text);
        toast.show();
    }



    /**
     * Make a standard toast that just contains a text view with the text from a resource.
     * @param resId The resource id of the string resource to use.  Can be formatted tex
     */
    public static void show(@StringRes int resId) {
        show(UtilManager.getContext().getResources().getString(resId));
    }

    /**
     * Make a standard toast that just contains a text view.
     *
     * @param text Toast str
     */
    public static void show(String text) {
        if (toast == null) {
            toast = Toast.makeText(UtilManager.getContext(), "", Toast.LENGTH_SHORT);
        }
        toast.setText(text);
        toast.show();
    }

}

@@ -310,42 +310,8 @@
  %7B%0A%0A
-    private static Toast toast;%0A%0A%0A
     
@@ -815,34 +815,19 @@
         
-if (toast == null)
+try
  %7B%0A     
@@ -825,32 +825,38 @@
 y %7B%0A            
+Toast 
 toast = Toast.ma
@@ -965,30 +965,24 @@
  0, 0);%0A    
-    %7D%0A
         toas
@@ -998,32 +998,36 @@
 (text);%0A        
+    
 toast.show();%0A  
@@ -1020,24 +1020,99 @@
 ast.show();%0A
+        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A
     %7D%0A%0A%0A%0A   
@@ -1603,26 +1603,11 @@
     
-if (toast == null)
+try
  %7B%0A 
@@ -1613,24 +1613,30 @@
             
+Toast 
 toast = Toas
@@ -1708,19 +1708,13 @@
     
- %7D%0A
      
-   
 toas
@@ -1730,32 +1730,36 @@
 (text);%0A        
+    
 toast.show();%0A  
@@ -1752,21 +1752,96 @@
 ast.show();%0A
+        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A
     %7D%0A%0A%7D%0A

优化 ToastUtils