Android Toast短时间内点击多次只显示一次,持续点击后不显示问题解决方案

 由于源码中的toast的持续时间为短toast2000,因此判断时间间隔大于2000则新new一个toast进行提示,否则如果toast消失了在进行setText()和show(),就显示不出toast了。

完整代码如下:

class ToastUtil {
    companion object {
        private var oldMsg: String? = null
        private var oneTime: Long = 0
        private var twoTime: Long = 0

        open fun showText(text: String) {
            if (text.isNullOrEmpty()) {
                return
            }

            if (oldMsg.isNullOrEmpty()) {
                // 没有弹出过toast
                Toast.makeText(BaseComponentApplication.sInstance, text, Toast.LENGTH_SHORT).show()
                oldMsg = text
                oneTime = System.currentTimeMillis()
            } else {
                twoTime = System.currentTimeMillis()
                if (twoTime - oneTime > 2000) {
                    // 判断toast上一次显示的时间和这次的显示时间如果大于2000
                    Toast.makeText(BaseComponentApplication.sInstance, text, Toast.LENGTH_SHORT)
                        .show()
                    oneTime = twoTime
                    oldMsg = text
                } else {
                    // 小余2000
                    if (!oldMsg.equals(text)) {
                        // 两次提示内容不一致,则提示
                        Toast.makeText(BaseComponentApplication.sInstance, text, Toast.LENGTH_SHORT)
                            .show()
                        oneTime = twoTime
                    }
                }
            }
        }

        open fun showText(stringsId: Int) {
            if (BaseComponentApplication.sInstance.getString(stringsId).isNullOrEmpty()) {
                return
            }

            if (oldMsg.isNullOrEmpty()) {
                // 没有弹出过toast
                Toast.makeText(BaseComponentApplication.sInstance, BaseComponentApplication.sInstance.getString(stringsId), Toast.LENGTH_SHORT).show()
                oldMsg = BaseComponentApplication.sInstance.getString(stringsId)
                oneTime = System.currentTimeMillis()
            } else {
                twoTime = System.currentTimeMillis()
                if (twoTime - oneTime > 2000) {
                    // 判断toast上一次显示的时间和这次的显示时间如果大于2000
                    Toast.makeText(
                        BaseComponentApplication.sInstance,
                        BaseComponentApplication.sInstance.getString(stringsId),
                        Toast.LENGTH_SHORT
                    ).show()
                    oneTime = twoTime
                    oldMsg = BaseComponentApplication.sInstance.getString(stringsId)
                } else {
                    // 小余2000
                    if (!oldMsg.equals(BaseComponentApplication.sInstance.getString(stringsId))) {
                        // 两次提示内容不一致,则提示
                        Toast.makeText(
                            BaseComponentApplication.sInstance,
                            BaseComponentApplication.sInstance.getString(stringsId),
                            Toast.LENGTH_SHORT
                        ).show()
                        oneTime = twoTime
                    }
                }
            }
        }
    }
}

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值