ToastUtil.java 5.07 KB
Newer Older
konghaorui committed
1 2 3 4 5
package com.yidianling.common.tools;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
konghaorui committed
6
import android.support.annotation.StringRes;
konghaorui committed
7 8 9 10 11 12 13 14
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

konghaorui committed
15 16
import com.yidianling.common.tools.support.toast.ToastCompat;

konghaorui committed
17 18 19 20 21 22 23 24 25 26 27 28 29
/**
 * 显示Toast工具类
 * Created by Dog on 2015/4/4.
 */
public class ToastUtil {


    private static String oldMsg;
    private static Handler handler;
    protected static Toast toast = null;
    private static long oneTime = 0;
    private static long twoTime = 0;

konghaorui committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    public static void toastShort(Context context, @StringRes Integer resourceId ) {
        toastShort(resourceId);
    }

    public static void toastShort(@StringRes Integer resourceId ) {
        try {
            String text = RxTool.getContext().getResources().getString(resourceId);
            toastShort(text);
        } catch (Exception e) {
            LogUtil.i(e.getMessage());
        }
    }

    public static void toastShort(Context context, String message) {
        safeToast(message);
    }
konghaorui committed
46 47 48 49 50 51 52

    public static void toastShort(String msg) {
        safeToast(msg);
    }

    private static void safeToast(String msg) {
        if(Looper.myLooper() == Looper.getMainLooper()) {
konghaorui committed
53
            showShortToast(RxTool.getContext(), msg);
konghaorui committed
54 55 56 57 58 59 60 61
        }else {
            //说明不是在主线程
            if (handler == null) {
                handler = new Handler(Looper.getMainLooper());
            }
            handler.post(() -> toastShort(RxTool.getContext(), msg));
        }
    }
konghaorui committed
62

konghaorui committed
63 64
    private static void showShortToast(Context mContext, String msg) {
        if (toast == null) {
konghaorui committed
65
            toast = ToastCompat.makeText(mContext.getApplicationContext(), msg, Toast.LENGTH_SHORT);
konghaorui committed
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
            centerText(toast.getView());
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (msg.equals(oldMsg)) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    centerText(toast.getView());
                    toast.show();
                }
            } else {
                oldMsg = msg;
                toast.setText(msg);
                centerText(toast.getView());
                toast.show();
            }
        }
        oneTime = twoTime;
    }


    private static View getView(Context context, Toast toast, String message) {
        View toastView = toast.getView();
        //创建一个ImageView
        TextView textView = new TextView(context);
        textView.setText(message);
        //创建一个LineLayout容器
        LinearLayout ll = new LinearLayout(context);
        ll.setGravity(Gravity.CENTER);
        ll.setPadding(20, 10, 20, 10);
        //向LinearLayout中添加ImageView和Toast原有的View
        ll.addView(textView);
        //ll.addView(toastView);
        //将LineLayout容器设置为toast的View
        return toastView;
    }

    public static void toastShortBottom(Context context, String message) {
        if (context != null) {
konghaorui committed
105
            Toast toast = ToastCompat.makeText(context, message, Toast.LENGTH_SHORT);
konghaorui committed
106 107 108 109 110 111 112
            centerText(toast.getView());
            toast.show();
        }
    }

    public static void toastLong(Context context, String message) {
        if (context != null) {
konghaorui committed
113
            Toast toast = ToastCompat.makeText(context, message, Toast.LENGTH_LONG);
konghaorui committed
114 115 116 117 118 119 120 121 122
            toast.setGravity(Gravity.CENTER, 0, 0);
            centerText(toast.getView());
            toast.show();

        }
    }

    public static void toastImg(Context context, int ImageResourceId) {
        //创建一个Toast提示消息
konghaorui committed
123
        Toast toast = ToastCompat.makeText(context, "", Toast.LENGTH_SHORT);
konghaorui committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        //设置Toast提示消息在屏幕上的位置
        toast.setGravity(Gravity.CENTER, 0, 0);
        //获取Toast提示消息里原有的View
        View toastView = toast.getView();
        //创建一个ImageView
        ImageView img = new ImageView(context);
        img.setImageResource(ImageResourceId);
        //创建一个LineLayout容器
        LinearLayout ll = new LinearLayout(context);
        //向LinearLayout中添加ImageView和Toast原有的View
        ll.addView(img);
        //ll.addView(toastView);
        //将LineLayout容器设置为toast的View
        toast.setView(ll);
        //显示消息
        toast.show();
    }

    static void centerText(View view) {
        if (view instanceof TextView) {
            ((TextView) view).setGravity(Gravity.CENTER);
            view.setPadding(20, 10, 20, 10);
        } else if (view instanceof ViewGroup) {
            ViewGroup group = (ViewGroup) view;

            if (view instanceof LinearLayout) {
                ((LinearLayout) view).setGravity(Gravity.CENTER);
            }
            int n = group.getChildCount();
            for (int i = 0; i < n; i++) {
                centerText(group.getChildAt(i));
            }
        }
    }


}