解决原始toast连续弹出时需要等待上个toast弹完才能弹出来的问题,
原理就是弹出新的toast前先取消之前的toast
import android.content.Context
import androidx.annotation.StringRes
import android.widget.Toast
import android.widget.Toast.LENGTH_SHORT
private var toast: Toast? = null
fun toast(context: Context, @StringRes resId: Int, duration: Int = LENGTH_SHORT) {
toast(context, context.getString(resId), duration)
}
fun toast(context: Context, text: String, duration: Int = LENGTH_SHORT) {
toast?.cancel()//取消之前的toast
toast = Toast.makeText(context, text, duration)//创建新toast
toast!!.show()
}
测试效果如下

本文介绍了一种解决Android中Toast连续弹出时出现延迟显示的方法。通过在弹出新Toast前取消上一个Toast,避免了等待上一个Toast结束的问题,有效提升了用户体验。

4792

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



