package com.tomtop.shop.utils;
import android.content.Context;
import android.widget.Toast;
import com.tomtop.shop.app.TTApplication;
/**
* @ClassName ToastUtil.java
* @Description 自定义toast
* @author tc
* @datetime 2015年8月20日下午4:04:50
*/
public class ToastUtil {
private static Toast mToast;
private static Context mContext = [你的application实现类]
.getApplicationContext();
/**
* 显示Toast,页面中重复Toast不会重复实例化Toast对象
*
* @param charSequence
* 字符串
*/
public static void showToast(CharSequence charSequence) {
if (mToast == null) {
mToast = Toast.makeText(mContext, charSequence, Toast.LENGTH_SHORT);
} else {
mToast.setText(charSequence);
mToast.setDuration(Toast.LENGTH_SHORT);
}
mToast.show();
}
/**
* 显示Toast,页面中重复Toast不会重复实例化Toast对象
*
* @param charSequence
* 字符串
* @param duration
* 显示时间
*/
public static void showToast(CharSequence charSequence, int duration) {
if (mToast == null) {
mToast = Toast.makeText(mContext, charSequence, duration);
} else {
mToast.setText(charSequence);
mToast.setDuration(duration);
}
mToast.show();
}
/**
* 显示Toast,页面中重复Toast不会重复实例化Toast对象
*
* @param resId
* String资源ID
* @param duration
* 显示时间
*/
public static void showToast(int resId, int duration) {
if (mToast == null) {
mToast = Toast.makeText(mContext, resId, duration);
} else {
mToast.setText(resId);
mToast.setDuration(duration);
}
mToast.show();
}
/**
* 显示Toast,页面中重复Toast不会重复实例化Toast对象
*
* @param resId
* String资源ID
*/
public static void showToast(int resId) {
if (mToast == null) {
mToast = Toast.makeText(mContext, resId, Toast.LENGTH_SHORT);
} else {
mToast.setText(resId);
mToast.setDuration(Toast.LENGTH_SHORT);
}
mToast.show();
}
/**
* 取消Toast显示
*/
public static void cancelToast() {
if (mToast != null) {
mToast.cancel();
}
}
}
全局ToastUtil
最新推荐文章于 2021-09-03 10:29:56 发布