ToastUtil吐司的工具类,防止多次吐司

食用于YAYA学车

public class ToastUtil {

    private static String oldMsg;
    private static long   time;
    private static int resOldMsg;
    public static void showMsg(Context context, String msg, int duration ) {
        if (!msg.equals(oldMsg)) { // 当显示的内容不一样时,即断定为不是同一个Toast
            Toast.makeText(context, msg, duration).show();
            time = System.currentTimeMillis();
        } else {
            // 显示内容一样时,只有间隔时间大于2秒时才显示
            if (System.currentTimeMillis() - time > 2000) {
                Toast.makeText(context, msg, duration).show();
                time = System.currentTimeMillis();
            }
        }
        oldMsg = msg;
    }

    public static void showMsg(Context context, String msg) {
        if (!msg.equals(oldMsg)) { // 当显示的内容不一样时,即断定为不是同一个Toast
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            time = System.currentTimeMillis();
        } else {
            // 显示内容一样时,只有间隔时间大于2秒时才显示
            if (System.currentTimeMillis() - time > 2000) {
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
                time = System.currentTimeMillis();
            }
        }
        oldMsg = msg;
    }

    public static void showMsg(Context context, int StringRes, int  duration) {
        if (StringRes!=resOldMsg) { // 当显示的内容不一样时,即断定为不是同一个Toast
            Toast.makeText(context, StringRes, duration).show();
            time = System.currentTimeMillis();
        } else {
            // 显示内容一样时,只有间隔时间大于2秒时才显示
            if (System.currentTimeMillis() - time > 2000) {
                Toast.makeText(context, StringRes, duration).show();
                time = System.currentTimeMillis();
            }
        }
        resOldMsg = StringRes;
    }

    public static void showMsg(Context context, int StringRes) {
        if (StringRes!=resOldMsg) { // 当显示的内容不一样时,即断定为不是同一个Toast
            Toast.makeText(context, StringRes, Toast.LENGTH_SHORT).show();
            time = System.currentTimeMillis();
        } else {
            // 显示内容一样时,只有间隔时间大于2秒时才显示
            if (System.currentTimeMillis() - time > 2000) {
                Toast.makeText(context, StringRes, Toast.LENGTH_SHORT).show();
                time = System.currentTimeMillis();
            }
        }
        resOldMsg = StringRes;
    }
}

eg。

   ToastUtil.showMsg(MainActivity.this, "网络不可用,请检查网络");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值