Toast每次弹出最新数据,相同数据避免重复弹出。

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:29   747   0

最终实现效果:每次都可以弹出最新的数据,相同数据避免重复弹出。

先看效果:

实现思路:

判断toast对象是否为空,为空则新建,不为空则先cancel再弹出,相关代码:

 if (toast == null) {
    /*toast为空新建*/
     toast = Toast.makeText(context, message, duration)
     toast?.setGravity(gravity, 0, 0)
} else {
    /*如果本次弹出数据与上一次不同,则取消正在弹出的toast,重新弹出*/
    toast?.cancel()
    toast = Toast.makeText(context, message, duration)
    toast?.setGravity(gravity, 0, 0)
}
 toast?.show()

重复弹出可以判断新数据和旧数据是否相同,如果相同则加上2s的时间限制,相关代码:

if (toast == null) {
   /*toast为空新建*/
   toast = Toast.makeText(context, message, duration)
   toast?.setGravity(gravity, 0, 0)
   time = System.currentTimeMillis()
 } else if (message.toString() != messageOld) {
   /*如果本次弹出数据与上一次不同,则取消正在弹出的toast,重新弹出*/
   toast?.cancel()
   toast = Toast.makeText(context, message, duration)
   toast?.setGravity(gravity, 0, 0)
   time = System.currentTimeMillis()
 } else {
   /*防止重复弹出*/
 if (System.currentTimeMillis() - time > 2000) {
    toast?.cancel()
    toast = Toast.makeText(context, message, duration)
    toast?.setGravity(gravity, 0, 0)
    time = System.currentTimeMillis()
   }
}
toast?.show()
messageOld = message.toString()

具体代码--Kotlin:

object ToastUtils {

    @JvmField
    var isShow = true

    @JvmField
    var messageOld: String? = null

    @JvmField
    var time: Long = 0

    @JvmField
    var toast: Toast? = null

    /**
     * 短时间显示Toast
     * @param context
     * @param message 显示字符串
     * @param gravity 显示位置
     */
    fun showShort(context: Context?, message: CharSequence, gravity: Int) {
        showToast(context, message, gravity, Toast.LENGTH_SHORT)
    }

    /**
     * 短时间显示Toast
     * @param context
     * @param message 显示字符串
     * @param gravity 显示位置
     * @param duration 持续时间
     */
    @SuppressLint("ShowToast")
    fun showToast(context: Context?, message: CharSequence, gravity: Int, duration: Int) {
        if (isShow) {
            if (toast == null) {
                /*toast为空新建*/
                toast = Toast.makeText(context, message, duration)
                toast?.setGravity(gravity, 0, 0)
                time = System.currentTimeMillis()
            } else if (message.toString() != messageOld) {
                /*如果本次弹出数据与上一次不同,则取消正在弹出的toast,重新弹出*/
                toast?.cancel()
                toast = Toast.makeText(context, message, duration)
                toast?.setGravity(gravity, 0, 0)
                time = System.currentTimeMillis()
            } else {
                /*防止重复弹出*/
                if (System.currentTimeMillis() - time > 2000) {
                    toast?.cancel()
                    toast = Toast.makeText(context, message, duration)
                    toast?.setGravity(gravity, 0, 0)
                    time = System.currentTimeMillis()
                }
            }
            toast?.show()
            messageOld = message.toString()
        }

    }

    /**
     * 短时间显示Toast
     * @param context
     * @param message 显示字符串
     */
    fun showShort(context: Context?, message: CharSequence) {
        showShort(context, message, Gravity.BOTTOM)
    }

    /**
     * 短时间显示Toast
     * @param context
     * @param message 显示字符串
     */
    fun showShortCenter(context: Context?, message: CharSequence) {
        showShort(context, message, Gravity.CENTER)
    }

    /**
     * 长时间Toast
     * @param context
     * @param message 显示字符串
     */
    fun showCenterLong(context: Context?, message: CharSequence) {
        showToast(context, message, Gravity.CENTER, Toast.LENGTH_LONG)
    }


    /**
     * 长时间Toast
     * @param context
     * @param message 显示字符串
     */
    fun showShortLong(context: Context?, message: CharSequence) {
        showToast(context, message, Gravity.BOTTOM, Toast.LENGTH_LONG)
    }

}

具体代码--Java:

public class ToastUtils {

    private ToastUtils() {
       
    }

    public static boolean isShow = true;
    public static String messageOld;
    public static long time;
    public static Toast toast;

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message 显示字符串
     * @param gravity 显示位置
     */
    @SuppressLint("ShowToast")
    public static void showShort(Context context, CharSequence message, int gravity) {
        showToast(context, message, gravity, Toast.LENGTH_SHORT);
    }

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message 显示字符串
     * @param gravity 显示位置
     */
    @SuppressLint("ShowToast")
    public static void showToast(Context context, CharSequence message, int gravity, int duration) {
        if (isShow) {
            if (toast == null) {
                // 如果Toast对象为空了,那么需要创建一个新的Toast对象
                toast = Toast.makeText(context, message, duration);
                toast.setGravity(gravity, 0, 0);
                time = System.currentTimeMillis();
            } else if (!message.toString().equals(messageOld)) {
                //如果本次弹出数据与上一次不同,则取消正在弹出的toast,重新弹出
                toast.cancel();
                toast = Toast.makeText(context, message, duration);
                toast.setGravity(gravity, 0, 0);
                time = System.currentTimeMillis();
            } else {
                /*防止重复弹出*/
                if (System.currentTimeMillis() - time > 2000) {
                    toast.cancel();
                    toast = Toast.makeText(context, message, duration);
                    toast.setGravity(gravity, 0, 0);
                    time = System.currentTimeMillis();
                }
            }
            toast.show();
            messageOld = message.toString();
        }
    }

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message 显示字符串
     */
    @SuppressLint("ShowToast")
    public static void showShort(Context context, CharSequence message) {
        showShort(context, message, Gravity.BOTTOM);
    }

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message 显示字符串
     */
    public static void showShortCenter(Context context, CharSequence message) {
        showShort(context, message, Gravity.CENTER);
    }

    /**
     * 长时间Toast
     *
     * @param context
     * @param message 显示字符串
     */
    @SuppressLint("ShowToast")
    public static void showCenterLong(Context context, CharSequence message) {
        showToast(context, message, Gravity.CENTER, Toast.LENGTH_LONG);
    }


 /**
  * 长时间Toast
  *
  * @param context
  * @param message 显示字符串
  */
 @SuppressLint("ShowToast")
 public static void showShortLong(Context context, CharSequence message) {
  showToast(context, message, Gravity.BOTTOM, Toast.LENGTH_LONG);
 }
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP