11package com.haoge.easyandroid.easy
22
33import android.annotation.SuppressLint
4+ import android.content.Context
45import android.os.Handler
56import android.os.Looper
67import android.text.TextUtils
@@ -18,8 +19,12 @@ import com.haoge.easyandroid.EasyAndroid
1819 */
1920class EasyToast private constructor(private val builder : Builder ) {
2021
22+ private val context: Context = EasyAndroid .getApplicationContext()
23+ private var toast: Toast ? = null
24+ private var tv: TextView ? = null
25+
2126 fun show (resId : Int ) {
22- show(EasyAndroid .getApplicationContext() .getString(resId))
27+ show(context .getString(resId))
2328 }
2429
2530 fun show (message : String? , vararg any : Any ) {
@@ -43,29 +48,29 @@ class EasyToast private constructor(private val builder:Builder) {
4348 createToastIfNeeded()
4449
4550 if (builder.isDefault) {
46- builder. toast?.setText(message)
47- builder. toast?.show()
51+ toast?.setText(message)
52+ toast?.show()
4853 } else {
49- builder. tv?.text = message
50- builder. toast?.show()
54+ tv?.text = message
55+ toast?.show()
5156 }
5257 }
5358
5459 @SuppressLint(" ShowToast" )
5560 private fun createToastIfNeeded () {
56- if (builder. toast == null ) {
61+ if (toast == null ) {
5762 if (builder.isDefault) {
58- builder. toast = Toast .makeText(EasyAndroid .getApplicationContext() , " " , Toast .LENGTH_SHORT )
63+ toast = Toast .makeText(context , " " , Toast .LENGTH_SHORT )
5964 } else {
60- val container = LayoutInflater .from(EasyAndroid .getApplicationContext() ).inflate(builder.layoutId, null )
61- builder. tv = container.findViewById(builder.tvId)
62- builder. toast = Toast (EasyAndroid .getApplicationContext() )
63- builder. toast?.view = container
64- builder. toast?.duration = builder.duration
65+ val container = LayoutInflater .from(context ).inflate(builder.layoutId, null )
66+ tv = container.findViewById(builder.tvId)
67+ toast = Toast (context )
68+ toast?.view = container
69+ toast?.duration = builder.duration
6570 }
6671
6772 if (builder.gravity != 0 ) {
68- builder. toast?.setGravity(builder.gravity, builder.offsetX, builder.offsetY)
73+ toast?.setGravity(builder.gravity, builder.offsetX, builder.offsetY)
6974 }
7075 }
7176 }
@@ -94,8 +99,6 @@ class EasyToast private constructor(private val builder:Builder) {
9499 class Builder (internal var isDefault : Boolean ,
95100 internal var layoutId : Int ,
96101 internal var tvId : Int ) {
97- internal var toast: Toast ? = null
98- internal var tv: TextView ? = null
99102
100103 internal var duration: Int = Toast .LENGTH_SHORT
101104 internal var gravity: Int = 0
0 commit comments