@@ -16,25 +16,7 @@ import com.haoge.easyandroid.EasyAndroid
1616 *
1717 * AUTHOR: haoge
1818 */
19- class EasyToast private constructor(private val layoutId : Int = -1 ,
20- private val tvId : Int = -1 ,
21- private val duration : Int = Toast .LENGTH_SHORT ,
22- private val isDefault : Boolean = true ) {
23-
24- private var toast: Toast ? = null
25- private var tv: TextView ? = null
26- private var gravity: Gravity ? = null
27-
28- fun reset () {
29- this .toast = null
30- this .tv = null
31- this .gravity = null
32- }
33-
34- fun setGravity (gravity : Int , offsetX : Int , offsetY : Int ):EasyToast {
35- this .gravity = EasyToast .Gravity (gravity, offsetX, offsetY)
36- return this
37- }
19+ class EasyToast private constructor(private val builder : Builder ) {
3820
3921 fun show (resId : Int ) {
4022 show(EasyAndroid .getApplicationContext().getString(resId))
@@ -60,31 +42,30 @@ class EasyToast private constructor(private val layoutId: Int = -1,
6042 private fun showInternal (message : String ) {
6143 createToastIfNeeded()
6244
63- val gravity = this .gravity
64- if (gravity != null ) {
65- toast?.setGravity(gravity.gravity, gravity.offsetX, gravity.offsetY)
66- this .gravity = null
67- }
68- if (isDefault) {
69- toast?.setText(message)
70- toast?.show()
45+ if (builder.isDefault) {
46+ builder.toast?.setText(message)
47+ builder.toast?.show()
7148 } else {
72- tv?.text = message
73- toast?.show()
49+ builder. tv?.text = message
50+ builder. toast?.show()
7451 }
7552 }
7653
7754 @SuppressLint(" ShowToast" )
7855 private fun createToastIfNeeded () {
79- if (toast == null ) {
80- if (isDefault) {
81- toast = Toast .makeText(EasyAndroid .getApplicationContext(), " " , Toast .LENGTH_SHORT )
56+ if (builder. toast == null ) {
57+ if (builder. isDefault) {
58+ builder. toast = Toast .makeText(EasyAndroid .getApplicationContext(), " " , Toast .LENGTH_SHORT )
8259 } else {
83- val container = LayoutInflater .from(EasyAndroid .getApplicationContext()).inflate(layoutId, null )
84- tv = container.findViewById(tvId)
85- toast = Toast (EasyAndroid .getApplicationContext())
86- toast?.view = container
87- toast?.duration = duration
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+ }
66+
67+ if (builder.gravity != 0 ) {
68+ builder.toast?.setGravity(builder.gravity, builder.offsetX, builder.offsetY)
8869 }
8970 }
9071 }
@@ -95,16 +76,46 @@ class EasyToast private constructor(private val layoutId: Int = -1,
9576 /* *
9677 * 默认提供的Toast实例,在首次使用时进行加载。
9778 */
98- val DEFAULT : EasyToast by lazy { default() }
79+ val DEFAULT : EasyToast by lazy { return @lazy newBuilder().build() }
80+
81+ fun newBuilder ():Builder {
82+ return Builder (true , 0 , 0 )
83+ }
9984
100- private fun default (): EasyToast {
101- return EasyToast (isDefault = true )
85+ fun newBuilder ( layoutId : Int , tvId : Int ): Builder {
86+ return Builder ( false , layoutId, tvId )
10287 }
10388
10489 fun create (layoutId : Int , tvId : Int , duration : Int ): EasyToast {
105- return EasyToast (layoutId, tvId, duration, isDefault = false )
90+ return newBuilder (layoutId, tvId).setDuration( duration).build( )
10691 }
10792 }
10893
109- private class Gravity (val gravity : Int , val offsetX : Int , val offsetY : Int )
94+ class Builder (internal var isDefault : Boolean ,
95+ internal var layoutId : Int ,
96+ internal var tvId : Int ) {
97+ internal var toast: Toast ? = null
98+ internal var tv: TextView ? = null
99+
100+ internal var duration: Int = Toast .LENGTH_SHORT
101+ internal var gravity: Int = 0
102+ internal var offsetX: Int = 0
103+ internal var offsetY: Int = 0
104+
105+ fun setGravity (gravity : Int , offsetX : Int , offsetY : Int ): Builder {
106+ this .gravity = gravity
107+ this .offsetX = offsetX
108+ this .offsetY = offsetY
109+ return this
110+ }
111+
112+ fun setDuration (duration : Int ): Builder {
113+ this .duration = duration
114+ return this
115+ }
116+
117+ fun build ():EasyToast {
118+ return EasyToast (this )
119+ }
120+ }
110121}
0 commit comments