package cn.sinata.xldutils.utils
|
|
import android.content.Context
|
import android.view.Gravity
|
import android.view.LayoutInflater
|
import android.widget.TextView
|
import android.widget.Toast
|
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.FragmentManager
|
import cn.sinata.xldutils.R
|
|
/**
|
* 扩展方法:允许DialogFragment使用 commitAllowingStateLoss 弹窗
|
*/
|
fun DialogFragment.showAllowingStateLoss(manager: FragmentManager, tag: String) {
|
val ft = manager.beginTransaction()
|
ft.add(this, tag)
|
ft.commitAllowingStateLoss()
|
}
|
|
fun Fragment.myToast(text: CharSequence, longTime:Boolean=false) = activity?.myToast(text,longTime)
|
|
fun Context.myToast(message: CharSequence,longTime:Boolean=false) {
|
val toast = Toast(this)
|
toast.duration = if (longTime)Toast.LENGTH_LONG else Toast.LENGTH_SHORT
|
toast.setGravity(Gravity.CENTER, 0, 0)
|
val toastLayout = LayoutInflater.from(this).inflate(R.layout.layout_center_toast, null)
|
val txtToast: TextView = toastLayout.findViewById(R.id.txt_toast)
|
txtToast.text = message
|
toast.view = toastLayout
|
toast.show()
|
}
|