自定义ToastUtils
Posted 天耀106
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义ToastUtils相关的知识,希望对你有一定的参考价值。
第二节:自定义ToastUtils
import android.annotation.SuppressLint
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.tian.yao.R
@SuppressLint("StaticFieldLeak")
object ToastUtils
private var toast: Toast? = null
private var toastView: View?=null
private var tvText: TextView?=null
/**
* 第一次时间
*/
private var oneTime: Long = 0
/**
* 第二次时间
*/
private var twoTime: Long = 0
/**
* 之前显示的内容
*/
private var oldMsg: String? = null
private var mContext:Context?=null
fun init(context: Context?)
mContext = context
fun showShortText(message: String)
if(mContext ==null)
return
showShortText(mContext, message)
/**
* 显示Toast
*
* @param context
* @param message
*/
fun showShortText(context: Context?, message: String)
if (toast == null)
toastView = LayoutInflater.from(context).inflate(R.layout.toast_text,null)
tvText = toastView?.findViewById<TextView>(R.id.tv_text)
tvText?.text = message
toast = Toast(context)
toast?.setGravity(Gravity.CENTER, 0, 0)
toast?.duration = Toast.LENGTH_SHORT
toast?.view = toastView
toast?.show()
oneTime = System.currentTimeMillis()
else
twoTime = System.currentTimeMillis()
if (message == oldMsg)
if (twoTime - oneTime > Toast.LENGTH_SHORT)
toast!!.show()
else
oldMsg = message
tvText!!.text = message
toast!!.show()
oneTime = twoTime
布局文件:toast_text
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/shape_toast">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingStart="28dp"
android:paddingTop="11dp"
android:paddingEnd="28dp"
android:paddingBottom="11dp"
android:text="测试!"
android:textColor="@color/white"
android:textSize="14sp" />
</LinearLayout>
drawable文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="140dp"
android:layout_height="42dp"
android:shape="rectangle"
tools:ignore="MissingDefaultResource">
<solid android:color="#FF6D6D6D" />
<corners android:radius="8dp" />
</shape>
App初始化:
import android.app.Application
import com.tian.yao.two.ToastUtils
class App :Application()
override fun onCreate()
super.onCreate()
ToastUtils.init(this.applicationContext)
Activity调用:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.tian.yao.two.ToastUtils
class MainActivity : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ToastUtils.showShortText("测试")
代码链接:
链接: https://pan.baidu.com/s/1aqpcuhVgIkCr2rfnDV_6ng 提取码: rrea
赞赏码,哈哈哈哈~
以上是关于自定义ToastUtils的主要内容,如果未能解决你的问题,请参考以下文章