ToastUtil.java 4.27 KB
package com.glsx.commonres.util;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.Toast;

import com.glsx.commonres.CommonResApp;
import com.blankj.utilcode.util.ToastUtils;
/**
 * Author by zh, Date on 2022/3/2.
 * PS: write a bug or more,please indicate.
 * Toast工具类
 */
public class ToastUtil {
    private static Toast lastToast = null;

    public final static int LENGTH_SHORT = Toast.LENGTH_SHORT;
    public final static int LENGTH_LONG = Toast.LENGTH_LONG;

    public static void clear() {
        if (lastToast != null) {
            lastToast.cancel();
        }
    }

    public static void show(int i) {
//        show(CommonResApp.getContext(), i);
        ToastUtils toastUtils = ToastUtils.make();
        toastUtils.setDurationIsLong(false);
        toastUtils.setBgColor(Color.BLACK);
        toastUtils.setTextColor(Color.WHITE);
        toastUtils.show(i);
    }

    public static void show(Context context, int i) {
        ToastUtils toastUtils = ToastUtils.make();
        toastUtils.setDurationIsLong(false);
        toastUtils.setBgColor(Color.BLACK);
        toastUtils.setTextColor(Color.WHITE);
        toastUtils.show(i);
//        show(context, context.getString(i), LENGTH_SHORT);
    }


    public static void showLongText( int i) {
        ToastUtils toastUtils = ToastUtils.make();
        toastUtils.setDurationIsLong(true);
        toastUtils.setBgColor(Color.BLACK);
        toastUtils.setTextColor(Color.WHITE);
        toastUtils.show(i);
//        Context context = CommonResApp.getContext();
//        show(context, context.getString(i), Toast.LENGTH_LONG);
    }
    public static void showLongText(  CharSequence charsequence) {
        ToastUtils toastUtils = ToastUtils.make();
        toastUtils.setDurationIsLong(true);
        toastUtils.setBgColor(Color.BLACK);
        toastUtils.setTextColor(Color.WHITE);
        toastUtils.show(charsequence);
//        Context context = CommonResApp.getContext();
//        show(context, charsequence, Toast.LENGTH_LONG);
    }

    public static void show(Context context, CharSequence charsequence) {
        ToastUtils toastUtils = ToastUtils.make();
        toastUtils.setDurationIsLong(true);
        toastUtils.setBgColor(Color.BLACK);
        toastUtils.setTextColor(Color.WHITE);
        toastUtils.show(charsequence);
//        show(context, charsequence, LENGTH_SHORT);
    }

    public static void show(CharSequence charsequence) {
        ToastUtils toastUtils = ToastUtils.make();
        toastUtils.setDurationIsLong(false);
        toastUtils.setBgColor(Color.BLACK);
        toastUtils.setTextColor(Color.WHITE);
        toastUtils.show(charsequence);

//        show(CommonResApp.getContext(), charsequence);
    }

    public static void showCenterTop(int msg) {
        ToastUtils toastUtils = ToastUtils.make();
        toastUtils.setGravity(Gravity.CENTER, 0, -300);
        toastUtils.setDurationIsLong(false);
        toastUtils.setBgColor(Color.BLACK);
        toastUtils.setTextColor(Color.WHITE);
        toastUtils.show(msg);

//        if (lastToast != null)
//            lastToast.cancel();
//        Context context = CommonResApp.getContext();
//        Toast toast = Toast.makeText(context, context.getString(msg), LENGTH_SHORT);
//        lastToast = toast;
//        toast.setGravity(Gravity.CENTER, 0, -300);
//        toast.show();
    }

    public static void showCenterTop(String msg) {
        ToastUtils toastUtils = ToastUtils.make();
        toastUtils.setGravity(Gravity.CENTER, 0, -300);
        toastUtils.setDurationIsLong(false);
        toastUtils.setBgColor(Color.BLACK);
        toastUtils.setTextColor(Color.WHITE);
        toastUtils.show(msg);
//        if (lastToast != null)
//            lastToast.cancel();
//        Context context = CommonResApp.getContext();
//        Toast toast = Toast.makeText(context, msg, LENGTH_SHORT);
//        lastToast = toast;
//        toast.setGravity(Gravity.CENTER, 0, -300);
//        toast.show();
    }
    private static final boolean isShow = false;

    /**
     * 调试使用
     *
     * @param charsequence
     */
    public static void showTest(CharSequence charsequence) {
        if (isShow)
            Toast.makeText(CommonResApp.getContext(), charsequence, Toast.LENGTH_SHORT).show();
    }
}