Kotlin探索之路(三)Toast工具類

如有問(wèn)題請(qǐng)指出

package com.summer.caidao.toast

import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.support.annotation.ColorInt
import android.support.annotation.DrawableRes
import android.support.annotation.LayoutRes
import android.support.annotation.StringRes
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.widget.Toast
import java.lang.ref.WeakReference


/**
 * Created by
 * User -> Summer
 * Date -> 2018/5/16
 *
 * Description: Toast工具類
 *
 */
const val DEFAULT_COLOR = 0x12000000

class CaidaoToast private constructor(context: Context) {

    private var mContext: Context = context

    private var backgroundColor = DEFAULT_COLOR
    private var messageColor = DEFAULT_COLOR
    private var mToast: Toast? = null
    private var bgResource = -1
    private var sViewWeakReference: WeakReference<View>? = null
    private var sHandler = Handler(Looper.getMainLooper())

    private var gravity = -1
    private var xOffset = -1
    private var yOffset = -1

    /**
     * 建造者模式,設(shè)置Toast的各種屬性
     */
    class Builder(context: Context) {

        private var caidaoToast = CaidaoToast(context)

        /**
         * 設(shè)置位置
         */
        fun setGravity(gravity: Int, xOffset: Int, yOffset: Int): Builder {
            caidaoToast.gravity = gravity
            caidaoToast.xOffset = xOffset
            caidaoToast.yOffset = yOffset
            return this
        }

        /**
         * 設(shè)置View
         */
        fun setView(@LayoutRes layoutId: Int): Builder {
            val inflate = caidaoToast.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            caidaoToast.sViewWeakReference = WeakReference(inflate.inflate(layoutId, null))
            return this
        }

        /**
         * 設(shè)置View
         */
        fun setView(view: View): Builder {
            caidaoToast.sViewWeakReference = WeakReference(view)
            return this
        }

        /**
         * 設(shè)置背景顏色
         */
        fun setBackgroundColor(@ColorInt backgroundColor: Int): Builder {
            caidaoToast.backgroundColor = backgroundColor
            return this
        }

        /**
         * 設(shè)置背景資源
         */
        fun setBgResource(@DrawableRes bgResource: Int): Builder {
            caidaoToast.bgResource = bgResource
            return this
        }

        /**
         * 設(shè)置消息字體顏色
         */
        fun setMessageColor(@ColorInt messageColor: Int): Builder {
            caidaoToast.messageColor = messageColor
            return this
        }

        /**
         * 建造者
         */
        fun build(): CaidaoToast {
            return caidaoToast
        }
    }

    /**
     * 安全地顯示短時(shí)吐司
     *
     * @param text 文本
     */
    fun showShortSafe(text: CharSequence) {
        sHandler.post { show(text, Toast.LENGTH_SHORT) }
    }

    /**
     * 安全地顯示短時(shí)吐司
     *
     * @param resId 資源Id
     */
    fun showShortSafe(@StringRes resId: Int) {
        sHandler.post { show(resId, Toast.LENGTH_SHORT) }
    }

    /**
     * 安全地顯示短時(shí)吐司
     *
     * @param resId 資源Id
     * @param args  參數(shù)
     */
    fun showShortSafe(@StringRes resId: Int, vararg args: Any) {
        sHandler.post { show(resId, Toast.LENGTH_SHORT, args) }
    }

    /**
     * 安全地顯示短時(shí)吐司
     *
     * @param format 格式
     * @param args   參數(shù)
     */
    fun showShortSafe(format: String, vararg args: Any) {
        sHandler.post { show(format, Toast.LENGTH_SHORT, args) }
    }

    /**
     * 安全地顯示長(zhǎng)時(shí)吐司
     *
     * @param text 文本
     */
    fun showLongSafe(text: CharSequence) {
        sHandler.post { show(text, Toast.LENGTH_LONG) }
    }

    /**
     * 安全地顯示長(zhǎng)時(shí)吐司
     *
     * @param resId 資源Id
     */
    fun showLongSafe(@StringRes resId: Int) {
        sHandler.post { show(resId, Toast.LENGTH_LONG) }
    }

    /**
     * 安全地顯示長(zhǎng)時(shí)吐司
     *
     * @param resId 資源Id
     * @param args  參數(shù)
     */
    fun showLongSafe(@StringRes resId: Int, vararg args: Any) {
        sHandler.post { show(resId, Toast.LENGTH_LONG, args) }
    }

    /**
     * 安全地顯示長(zhǎng)時(shí)吐司
     *
     * @param format 格式
     * @param args   參數(shù)
     */
    fun showLongSafe(format: String, vararg args: Any) {
        sHandler.post { show(format, Toast.LENGTH_LONG, args) }
    }

    /**
     * 顯示短時(shí)吐司
     *
     * @param text 文本
     */
    fun showShort(text: CharSequence) {
        show(text, Toast.LENGTH_SHORT)
    }

    /**
     * 顯示短時(shí)吐司
     *
     * @param resId 資源Id
     */
    fun showShort(@StringRes resId: Int) {
        show(resId, Toast.LENGTH_SHORT)
    }

    /**
     * 顯示短時(shí)吐司
     *
     * @param resId 資源Id
     * @param args  參數(shù)
     */
    fun showShort(@StringRes resId: Int, vararg args: Any) {
        show(resId, Toast.LENGTH_SHORT, args)
    }

    /**
     * 顯示短時(shí)吐司
     *
     * @param format 格式
     * @param args   參數(shù)
     */
    fun showShort(format: String, vararg args: Any) {
        show(format, Toast.LENGTH_SHORT, args)
    }

    /**
     * 顯示長(zhǎng)時(shí)吐司
     *
     * @param text 文本
     */
    fun showLong(text: CharSequence) {
        show(text, Toast.LENGTH_LONG)
    }

    /**
     * 顯示長(zhǎng)時(shí)吐司
     *
     * @param resId 資源Id
     */
    fun showLong(@StringRes resId: Int) {
        show(resId, Toast.LENGTH_LONG)
    }

    /**
     * 顯示長(zhǎng)時(shí)吐司
     *
     * @param resId 資源Id
     * @param args  參數(shù)
     */
    fun showLong(@StringRes resId: Int, vararg args: Any) {
        show(resId, Toast.LENGTH_LONG, args)
    }

    /**
     * 顯示長(zhǎng)時(shí)吐司
     *
     * @param format 格式
     * @param args   參數(shù)
     */
    fun showLong(format: String, vararg args: Any) {
        show(format, Toast.LENGTH_LONG, args)
    }

    /**
     * 顯示吐司
     *
     * @param resId    資源Id
     * @param duration 顯示時(shí)長(zhǎng)
     */
    private fun show(@StringRes resId: Int, duration: Int) {
        show(mContext.getString(resId), duration)
    }

    /**
     * 顯示吐司
     *
     * @param resId    資源Id
     * @param duration 顯示時(shí)長(zhǎng)
     * @param args     參數(shù)
     */
    private fun show(@StringRes resId: Int, duration: Int, vararg args: Any) {
        show(String.format(mContext.getString(resId), args), duration)
    }

    /**
     * 顯示吐司
     *
     * @param format   格式
     * @param duration 顯示時(shí)長(zhǎng)
     * @param args     參數(shù)
     */
    private fun show(format: String, duration: Int, vararg args: Any) {
        show(String.format(format, *args), duration)
    }

    /**
     * 顯示吐司
     *
     * @param text     文本
     * @param duration 顯示時(shí)長(zhǎng)
     */
    @SuppressLint("ShowToast")
    private fun show(text: CharSequence, duration: Int) {
        cancel()
        if (text.isBlank()) return

        var isCustom = false
        val customView = sViewWeakReference?.get()
        if (null != customView) {
            mToast = Toast(mContext)
            mToast?.view = customView
            mToast?.duration = duration
            isCustom = true
        }

        if (!isCustom) {
            mToast = if (messageColor != DEFAULT_COLOR) {
                val spannableString = SpannableString(text)
                val colorSpan = ForegroundColorSpan(messageColor)
                spannableString.setSpan(colorSpan, 0, spannableString.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                Toast.makeText(mContext, spannableString, duration)
            } else {
                Toast.makeText(mContext, text, duration)
            }
        }

        val toastView = mToast?.view
        if (bgResource != -1) {
            toastView?.setBackgroundResource(bgResource)
        } else if (backgroundColor != DEFAULT_COLOR) {
            toastView?.setBackgroundColor(backgroundColor)
        }
        if (-1 != gravity && -1 != xOffset && -1 != yOffset) {
            mToast?.setGravity(gravity, xOffset, yOffset)
        }
        mToast?.show()
    }

    /**
     * 取消吐司顯示
     */
    private fun cancel() {
        mToast?.cancel()
        mToast = null
    }


}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容