ToastUtils 吐司工具类
吐司工具类(使用GraceToast时,自定义吐司布局中显示消息的TextView的ID须设置为"@+id/tv_msg")
object ToastUtils {
private var toast: Toast? = null
private val handler = Handler()
private val r = Runnable { toast!!.cancel() }
fun toastLong(msg: String) {
toast(msg, Toast.LENGTH_LONG)
}
fun toastLong(@StringRes msgID: Int) {
toast(msgID, Toast.LENGTH_LONG)
}
fun toastShort(msg: String) {
toast(msg, Toast.LENGTH_SHORT)
}
fun toastShort(@StringRes msgID: Int) {
toast(msgID, Toast.LENGTH_SHORT)
}
private fun toast(msg: String, duration: Int) {
toast(msg, duration.toLong())
}
private fun toast(@StringRes msgID: Int, duration: Int) {
toast(msgID, duration.toLong())
}
fun toast(@StringRes msgID: Int, duration: Long) {
toast(ResourcesUtils.getString(msgID), duration)
}
fun toast(msg: String, duration: Long) {
if (duration != 1L && duration != 0L) {
handler.removeCallbacks(r)
}
if (null == toast) {
toast = Toast.makeText(MyAPP.getInstance(), msg, Toast.LENGTH_LONG)
} else {
toast!!.setText(msg)
}
if ("main".equals(Thread.currentThread().name, ignoreCase = true)) {
showToast(duration)
} else {
ActivityManager.getCurrentActivity().runOnUiThread({
showToast(duration)
})
}
}
private fun showToast(duration: Long) {
if (duration == 1L || duration == 0L) {
toast!!.duration = if (duration == 1L) Toast.LENGTH_LONG else Toast.LENGTH_SHORT
} else {
handler.postDelayed(r, duration)
}
toast!!.show()
}
fun graceToastLong(msg: String, @LayoutRes layoutID: Int) {
graceToast(msg, layoutID, Toast.LENGTH_LONG)
}
fun graceToastLong(@StringRes msgID: Int, @LayoutRes layoutID: Int) {
graceToast(msgID, layoutID, Toast.LENGTH_LONG)
}
fun graceToastShort(msg: String, @LayoutRes layoutID: Int) {
graceToast(msg, layoutID, Toast.LENGTH_SHORT)
}
fun graceToastShort(@StringRes msgID: Int, @LayoutRes layoutID: Int) {
graceToast(msgID, layoutID, Toast.LENGTH_SHORT)
}
private fun graceToast(@StringRes msgID: Int, @LayoutRes layoutID: Int, duration: Int): View {
return ToastUtils.graceToast(msgID, layoutID, duration.toLong())
}
fun graceToast(msg: String, @LayoutRes layoutID: Int, duration: Int): View {
return ToastUtils.graceToast(msg, layoutID, duration.toLong())
}
fun graceToast(@StringRes msgID: Int, @LayoutRes layoutID: Int, duration: Long): View {
return ToastUtils.graceToast(ResourcesUtils.getString(msgID), layoutID, duration)
}
private fun graceToast(msg: String, @LayoutRes layoutID: Int, duration: Long): View {
return ToastUtils.graceToast(msg, layoutID, Gravity.BOTTOM, 0, DensityUtils.dip2px(64f), duration)
}
fun graceToast(@StringRes msgID: Int, @LayoutRes layoutID: Int, gravity: Int, xOffset: Int, yOffset: Int, duration: Long): View {
return ToastUtils.graceToast(ResourcesUtils.getString(msgID), layoutID, gravity, xOffset, yOffset, duration)
}
fun graceToast(msg: String, @LayoutRes layoutID: Int, gravity: Int, xOffset: Int, yOffset: Int, duration: Long): View {
val view = View.inflate(MyAPP.getInstance(), layoutID, null)
if (duration != 1L && duration != 0L) {
handler.removeCallbacks(r)
}
view.findViewById<TextView>(R.id.tv_msg).setText(msg)
if (null == toast) {
toast = Toast(MyAPP.getInstance())
toast!!.duration = Toast.LENGTH_SHORT
toast!!.view = view
toast!!.setGravity(gravity, xOffset, yOffset)
}
if ("main".equals(Thread.currentThread().name, ignoreCase = true)) {
showToast(duration)
} else {
ActivityManager.getCurrentActivity().runOnUiThread(Runnable { showToast(duration) })
}
return view
}
}