package cn.flightfeather.thirdapp.util
|
|
import android.app.Activity
|
import android.app.Dialog
|
import android.content.Context
|
import android.content.DialogInterface
|
import android.support.annotation.LayoutRes
|
import android.view.Gravity
|
import android.view.LayoutInflater
|
import android.view.View
|
import android.view.ViewGroup
|
import android.widget.PopupWindow
|
import android.widget.TextView
|
import cn.flightfeather.thirdapp.R
|
import cn.flightfeather.thirdapp.module.base.BaseActivity
|
import cn.flightfeather.thirdapp.view.PopupWindowWithMask
|
|
/**
|
* @author riku
|
* Date: 2020/5/14
|
*/
|
object DialogUtil2 {
|
|
fun showAlertDialog(
|
context: Context?,
|
content: String,
|
onConfirmed: (dialog: Dialog) -> Unit ={ it.dismiss()},
|
onCanceled: (dialog: Dialog) -> Unit = {it.dismiss() }
|
): Dialog? {
|
val view = LayoutInflater.from(context).inflate(R.layout.dialog_alert_2, null)
|
return context?.let {
|
Dialog(it).apply {
|
setCancelable(true)
|
setCanceledOnTouchOutside(true)
|
setContentView(view)
|
view.findViewById<TextView>(R.id.tv_dialog_content).text = content
|
view.findViewById<TextView>(R.id.tv_positive).setOnClickListener { onConfirmed(this) }
|
view.findViewById<TextView>(R.id.tv_neutral).setOnClickListener { onCanceled(this) }
|
window?.attributes?.apply {
|
gravity = Gravity.CENTER
|
width = (ScreenUtils.getScreenWidth(context) * 0.8).toInt()
|
windowAnimations = R.style.dialog_anim_style
|
}
|
window?.setBackgroundDrawableResource(android.R.color.transparent)
|
window?.setGravity(Gravity.CENTER)
|
show()
|
}
|
}
|
}
|
|
fun showBottomDialog(
|
activity: Activity?,
|
anchorView: View,
|
textList: List<String>,
|
listeners: List<(popupWindow: PopupWindow) -> Unit>,
|
onCanceled: (popupWindow: PopupWindow) -> Unit = {}
|
) {
|
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_bottom_sheet, null)
|
activity?.let {
|
PopupWindowWithMask(it).apply popupWindow@{
|
isFocusable = true
|
isOutsideTouchable = true
|
contentView = view
|
view.findViewById<TextView>(R.id.txt_choice_1).apply {
|
if (textList.isNotEmpty() && listeners.isNotEmpty()) {
|
text = textList.first()
|
setOnClickListener { listeners.first().invoke(this@popupWindow) }
|
}
|
}
|
view.findViewById<TextView>(R.id.txt_choice_2).apply {
|
if (textList.size > 1 && listeners.size > 1) {
|
text = textList[1]
|
setOnClickListener { listeners[1].invoke(this@popupWindow) }
|
}
|
}
|
view.findViewById<TextView>(R.id.txt_cancel).setOnClickListener {
|
onCanceled(this)
|
dismiss()
|
}
|
height = ViewGroup.LayoutParams.WRAP_CONTENT
|
width = ViewGroup.LayoutParams.MATCH_PARENT
|
animationStyle = R.style.PopWin_bottom_anim_style
|
background.alpha = 0
|
}.run {
|
showAtLocation(anchorView, Gravity.BOTTOM, 0, 0)
|
}
|
}
|
}
|
|
class Builder constructor(private val context: Context){
|
var view: View = LayoutInflater.from(context).inflate(R.layout.dialog_alert, null)
|
private val dialog = Dialog(context).apply {
|
setCancelable(true)
|
setCanceledOnTouchOutside(true)
|
setContentView(view)
|
window?.setBackgroundDrawableResource(android.R.color.transparent)
|
window?.attributes?.apply {
|
gravity = Gravity.CENTER
|
width = (ScreenUtils.getScreenWidth(context) * 0.9).toInt()
|
}
|
window?.setGravity(Gravity.CENTER)
|
}
|
|
fun setContentView(@LayoutRes layoutResID: Int) {
|
view = LayoutInflater.from(context).inflate(layoutResID, null)
|
dialog.setContentView(view)
|
}
|
|
fun setTitle(t: String) {
|
view.findViewById<TextView>(R.id.tv_dialog_title).text = t
|
}
|
|
fun setContent(c: String) {
|
view.findViewById<TextView>(R.id.tv_dialog_content).text = c
|
}
|
|
fun setPositiveButton(text: String, l: DialogInterface.OnClickListener) {
|
view.findViewById<TextView>(R.id.tv_positive).apply {
|
this.text = text
|
setOnClickListener{
|
l.onClick(dialog, 0)
|
}
|
}
|
}
|
|
fun setNegativeButton(text: String, l: DialogInterface.OnClickListener) {
|
view.findViewById<TextView>(R.id.tv_negative).apply {
|
this.text = text
|
setOnClickListener{
|
l.onClick(dialog, 1)
|
}
|
}
|
}
|
|
fun setNeutralButton(text: String, l: DialogInterface.OnClickListener) {
|
view.findViewById<TextView>(R.id.tv_neutral).apply {
|
this.text = text
|
setOnClickListener{
|
l.onClick(dialog, 2)
|
}
|
}
|
}
|
|
fun create(): Dialog = dialog
|
}
|
}
|