APP一般Toast主要有三种,正常的、成功的、错误的。
分别为每种设计一种布局,传入提示的 text 就可以了,同时限制了相同 text 的Toast 2 秒内不能重复提示!
package com.youthmba.ymbaandroid.appbiz.basics
import android.content.Context
import android.text.TextUtils
import android.view.Gravity.CENTER
import android.view.LayoutInflater
import android.widget.TextView
import android.widget.Toast
import com.youthmba.ymbaandroid.R
import com.youthmba.ymbaandroid.appbiz.Application
public class NewToastUtils {
companion object {
var lastToastTime = 0L
var lastToastString = ""
@JvmStatic
fun toastNormal(text:String?) {
if(!TextUtils.isEmpty(text)){
toast(text!!,R.layout.toast_normal)
}
}
@JvmStatic
fun toastSuccess(text:String?) {
if(!TextUtils.isEmpty(text)){
toast(text!!,R.layout.toast_success)
}
}
@JvmStatic
fun toastError(text:String?) {
if(!TextUtils.isEmpty(text)){
toast(text!!,R.layout.toast_error)
}
}
@JvmStatic
private fun toast(text:String,layoutId:Int) {
if(text != lastToastString || System.currentTimeMillis() - lastToastTime > 2000L) {
val toast = Toast(Application.getAppContext())
toast.view = LayoutInflater.from(Application.getAppContext()).inflate(layoutId, null)
toast.view.findViewById(R.id.tvContent).text = text
toast.duration = Toast.LENGTH_SHORT
toast.setGravity(CENTER, 0, 0)
toast.show()
lastToastTime = System.currentTimeMillis()
lastToastString = text
}
}
}
}
使用:
NewToastUtils.toastSuccess("成功了")
NewToastUtils.toastError("失败了")
NewToastUtils.toastNormal("普通提示")



