Toast自定义
public class ToastUtil {
private static final long TOAST_THRESHOLD = 2000;
private static long previous = 0;
private static Toast toast;
private static Context context;
private static TextView tipTv;
private ToastUtil() {}
public static void init(Context ctx) {
context = ctx;
}
public static void toastLong(String message) {
toast(message, Toast.LENGTH_LONG);
}
public static void toast(String message) {
toast(message, Toast.LENGTH_SHORT);
}
public static void toast(int id) {
toast(id, Toast.LENGTH_SHORT);
}
public static void toast(int id, int duration) {
String message = context.getString(id);
toast(message, duration);
}
public static void toast(String text, int duration) {
long now = System.currentTimeMillis();
if (now - previous < TOAST_THRESHOLD) {
tipTv.setText(text);
toast.show();
} else {
if (toast != null) {
toast.cancel();
}
toast = new Toast(context);
View view = LayoutInflater.from(context).inflate(R.layout.view_toast, null);
tipTv = (TextView) view.findViewById(R.id.toast_text);
tipTv.setText(text);
toast.setDuration(duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setView(view);
toast.show();
}
previous = now;
}
public static void toast(String text, int duration, int xOffset, int yOffset) {
long now = System.currentTimeMillis();
if (now - previous < TOAST_THRESHOLD) {
tipTv.setText(text);
toast.show();
} else {
if (toast != null) {
toast.cancel();
}
toast = new Toast(context);
View view = LayoutInflater.from(context).inflate(R.layout.view_toast, null);
tipTv = (TextView) view.findViewById(R.id.toast_text);
tipTv.setText(text);
toast.setDuration(duration);
toast.setView(view);
toast.setGravity(Gravity.NO_GRAVITY, xOffset, yOffset);
toast.show();
}
previous = now;
}
public static void cancel() {
if (toast != null) {
toast.cancel();
}
}
}