初始代码提交
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package com.sw.inbound.dialog
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.graphics.Color
|
||||
import android.view.LayoutInflater
|
||||
import androidx.core.graphics.drawable.toDrawable
|
||||
import com.sw.inbound.R
|
||||
import com.sw.inbound.databinding.DialogLoadingBinding
|
||||
import kotlin.let
|
||||
import kotlin.run
|
||||
|
||||
object Loading {
|
||||
|
||||
private var dialog: LoadingDialog? = null
|
||||
|
||||
fun show(activity: Activity) {
|
||||
try {
|
||||
if (activity.isFinishing || activity.isDestroyed) {
|
||||
return
|
||||
}
|
||||
if (dialog?.isShowing == true) {
|
||||
dialog?.dismiss()
|
||||
}
|
||||
if (dialog == null) {
|
||||
dialog = LoadingDialog(activity)
|
||||
}
|
||||
dialog?.show()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
fun dismiss() {
|
||||
try {
|
||||
dialog?.let {
|
||||
if (it.isShowing) {
|
||||
it.dismiss()
|
||||
}
|
||||
}
|
||||
dialog = null
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class LoadingDialog(
|
||||
context: Context,
|
||||
message: String = "加载中……",
|
||||
private var onDismiss: () -> Unit = {}
|
||||
) : Dialog(context, R.style.LoadingDialog) {
|
||||
init {
|
||||
val binding = DialogLoadingBinding.inflate(LayoutInflater.from(context))
|
||||
binding.tvMessage.text = message
|
||||
setContentView(binding.root)
|
||||
setCancelable(true)
|
||||
setCanceledOnTouchOutside(true)
|
||||
window?.run {
|
||||
setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
|
||||
// setFlags(
|
||||
// WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
|
||||
// WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|
||||
// )
|
||||
}
|
||||
setOnDismissListener {
|
||||
onDismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user