puzhibing
2026-08-21 5fa94b3718b4a78a4fcea4c97652b0ef20b7de3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.xw.common.utils
 
import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.os.Looper
import com.xw.common.utils.LogUtils
import android.view.Gravity
import android.view.View
import android.widget.Toast
import androidx.appcompat.widget.AppCompatTextView
import com.xw.common.R
 
@SuppressLint("SoonBlockedPrivateApi")
class ToastUtilCommon(var context: Context) {
    var toast: Toast? = null
    var msg: String? = null
 
    @SuppressLint("UseCompatLoadingForDrawables")
    fun create(duration: Int, id: Int?): Toast {
        if (toast == null) {
            toast = Toast(context)
            val contentView = View.inflate(context, R.layout.dialog_toast, null)
            toast!!.view = contentView
            toast!!.setGravity(Gravity.CENTER, 0, 0)
        }
        val tvMsg = toast!!.view!!.findViewById<AppCompatTextView>(R.id.tv_toast_msg)
        toast!!.duration = duration
        tvMsg.text = msg
 
        val drawableImg = id?.let {
            context.resources.getDrawable(id, context.theme)
        }
        drawableImg?.setBounds(0, 0, drawableImg.minimumWidth, drawableImg.minimumHeight)
        tvMsg.setCompoundDrawables(drawableImg, null, null, null)
 
        return toast!!
    }
 
    companion object {
        @SuppressLint("StaticFieldLeak")
        var td: ToastUtilCommon? = null
 
        @JvmStatic
        fun show(msg: String?, duration: Int = Toast.LENGTH_SHORT, drawableId: Int? = null) {
            msg?.let {
                if (msg.isNotEmpty()) {
                    if (msg == "Job was cancelled") {
                        return
                    }
 
                    val tips = if (msg.startsWith("failed to connect to")) {
                        "无法连接服务器"
                    } else {
                        msg
                    }
                    try {
                        Handler(Looper.getMainLooper()).post {
                            if (td == null) {
                                td = ToastUtilCommon(AppGlobals.getApplication())
                            }
                            td!!.msg = tips
                            val t = td!!.create(duration, drawableId)
                            t.show()
                        }
                    } catch (e: Exception) {
                        if (Looper.myLooper() == null) {
                            Looper.prepare()
                            if (td == null) {
                                td = ToastUtilCommon(AppGlobals.getApplication())
                            }
                            td!!.msg = tips
                            val t = td!!.create(duration, drawableId)
                            t.show()
                            Looper.loop()
                        }
                    }
                }
            }
        }
 
        fun showSuc(msg: String?, duration: Int = Toast.LENGTH_SHORT) {
            show(msg, duration, R.drawable.svg_toast_suc)
        }
 
        fun showError(msg: String?, duration: Int = Toast.LENGTH_SHORT) {
            show(msg, duration, R.drawable.svg_toast_error)
        }
 
        fun showWarn(msg: String?, duration: Int = Toast.LENGTH_SHORT){
            show(msg, duration, R.drawable.svg_toast_warn)
        }
    }
}