Skip to content

Commit 06f559f

Browse files
committed
Add getView for EasyToast
1 parent c8dd640 commit 06f559f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

utils/src/main/java/com/haoge/easyandroid/easy/EasyToast.kt

+11-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class EasyToast private constructor(private val builder:Builder) {
3838
private val context: Context = EasyAndroid.getApplicationContext()
3939
private var toast:Toast? = null
4040
private var tv:TextView? = null
41+
private var container:View? = null
4142

4243
fun show(resId:Int) {
4344
show(context.getString(resId))
@@ -60,6 +61,14 @@ class EasyToast private constructor(private val builder:Builder) {
6061
}
6162
}
6263

64+
/** 获取Toast的View进行使用,只支持自定义样式的Toast,若为系统默认Toast样式,则返回null*/
65+
fun getView():View? {
66+
if (container == null && builder.isDefault.not()) {
67+
createToastIfNeeded()
68+
}
69+
return container
70+
}
71+
6372
private fun showInternal(message: String) {
6473
createToastIfNeeded()
6574

@@ -78,8 +87,8 @@ class EasyToast private constructor(private val builder:Builder) {
7887
if (builder.isDefault) {
7988
toast = Toast.makeText(context, "", builder.duration)
8089
} else {
81-
val container = builder.layout?:LayoutInflater.from(context).inflate(builder.layoutId, null)
82-
tv = container.findViewById(builder.tvId)
90+
container = builder.layout?:LayoutInflater.from(context).inflate(builder.layoutId, null)
91+
tv = container?.findViewById(builder.tvId)
8392
toast = Toast(context)
8493
toast?.view = container
8594
toast?.duration = builder.duration

0 commit comments

Comments
 (0)