class ToastUtils {
private static String oldMsg;
/** Toast对象 */
private static Toast toast = null;
/** 第一次时间 */
private static long oneTime = 0;
/** 第二次时间 */
private static long twoTime = 0;
/**
* 显示Toast
*
* @param context
* @param message
*/
public static void showToast(Context context, String message) {
if (toast == null) {
toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toast.show();
oneTime = System.currentTimeMillis();
} else {
twoTime = System.currentTimeMillis();
if (message.equals(oldMsg)) {
if (twoTime - oneTime > Toast.LENGTH_SHORT) {
toast.show();
}
} else {
oldMsg = message;
toast.setText(message);
toast.show();
}
}
oneTime = twoTime;
}
}
private static String oldMsg;
/** Toast对象 */
private static Toast toast = null;
/** 第一次时间 */
private static long oneTime = 0;
/** 第二次时间 */
private static long twoTime = 0;
/**
* 显示Toast
*
* @param context
* @param message
*/
public static void showToast(Context context, String message) {
if (toast == null) {
toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toast.show();
oneTime = System.currentTimeMillis();
} else {
twoTime = System.currentTimeMillis();
if (message.equals(oldMsg)) {
if (twoTime - oneTime > Toast.LENGTH_SHORT) {
toast.show();
}
} else {
oldMsg = message;
toast.setText(message);
toast.show();
}
}
oneTime = twoTime;
}
}
本文介绍了一个名为 ToastUtils 的工具类,用于管理 Android 应用中的 Toast 显示逻辑。该工具类可以避免短时间内重复显示相同内容,并能智能更新显示内容。
7734

被折叠的 条评论
为什么被折叠?



