一个自定义倒计时对话框,CountDownDialog
效果
实现
kotlin
/**
* 作用: 自定义弹框
* 用法: val builder = CountDownDialog.Builder(this)
* builder.apply {
* setTitle("1")
* setContent("2")
* setTheme("3")
* setTime("4")
* setHost("5")
* setQRImage(resources.getDrawable(R.mipmap.default_person))
* setPositiveButton(DialogInterface.OnClickListener { dialog, _ ->
* dialog.dismiss()
* })
* create().show()}
*/
class CountDownDialog : Dialog {
constructor(context: Context?) : super(context!!)
constructor(context: Context?, theme: Int) : super(context!!, theme)
class Builder(private val context: Context) {
private lateinit var textCountDown: TextView
private lateinit var countDownDialog: CountDownDialog
private var countDownTime = 15
private var countDownHandler = Handler()
private val countDown = object : Runnable {
override fun run() {
if (countDownTime >= 0) {
countDownHandler.postDelayed(this, 1000)//每秒
//执行UI更新逻辑
textCountDown.text = "${countDownTime}s"
} else {
//停止的逻辑
countDownHandler.removeCallbacksAndMessages(null)
countDownDialog.dismiss()
}
countDownTime--
}
}
private var mTitle: String? = null
private var mContent: String? = null
private var mTheme: String? = null
private var mTime: String? = null
private var mHost: String? = null
private var mQRImage: Drawable? = null
private var positiveButtonClickListener: DialogInterface.OnClickListener? = null
//设置标题
fun setTitle(title: String): Builder {
this.mTitle = title
return this
}
//设置会议室文本
fun setContent(content: String): Builder {
this.mContent = content
return this
}
//设置主题
fun setTheme(theme: String): Builder {
this.mTheme = theme
return this
}
//设置时间
fun setTime(time: String): Builder {
this.mTime = time
return this
}
//设置发起人
fun setHost(host: String): Builder {
this.mHost = host
return this
}
//设置二维码图标
fun setQRImage(qrImage: Drawable): Builder {
this.mQRImage = qrImage
return this
}
fun setPositiveButton(listener: DialogInterface.OnClickListener): Builder {
this.positiveButtonClickListener = listener
return this
}
fun create(): CountDownDialog {
val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
//为自定义弹框设置主题
countDownDialog = CountDownDialog(context, R.style.CustomDialog)
val view = layoutInflater.inflate(R.layout.dialog_champer_order_success, null)
countDownDialog.addContentView(view, LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
))
//设置倒计时
textCountDown = view.findViewById(R.id.dialog_champer_countdown)
textCountDown.text = "15s"
//设置标题
mTitle?.let {
(view.findViewById(R.id.dialog_champer_title) as TextView).text = it
}
//设置内容
mContent?.let {
(view.findViewById(R.id.dialog_champer_content) as TextView).text = it
}
//设置主题
mTheme?.let {
(view.findViewById(R.id.dialog_champer_theme) as TextView).text = it
}
//设置时间
mTime?.let {
(view.findViewById(R.id.dialog_champer_time) as TextView).text = it
}
//设置发起人
mHost?.let {
(view.findViewById(R.id.dialog_champer_host) as TextView).text = it
}
//设置二维码图标
mQRImage?.let {
(view.findViewById(R.id.dialog_champer_qr) as ImageView).setImageDrawable(it)
}
//设置弹框按钮
positiveButtonClickListener?.let {
(view.findViewById(R.id.dialog_champer_close) as ImageView).setOnClickListener {
countDownHandler.removeCallbacksAndMessages(null)
positiveButtonClickListener!!.onClick(countDownDialog, DialogInterface.BUTTON_POSITIVE)
}
} ?: run {
(view.findViewById(R.id.dialog_champer_close) as ImageView).visibility = View.GONE
}
countDownDialog.setContentView(view)
countDownTime = 15
countDownHandler.postDelayed(countDown, 0)
return countDownDialog
}
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="100dp"
android:background="@drawable/champer_form"
android:orientation="vertical"
android:layout_gravity="center">
<!--标题栏-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/dialog_champer_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="会议发起成功"
android:textColor="#060D4F"
android:textSize="@dimen/title"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<ImageView
android:id="@+id/dialog_champer_close"
android:layout_width="@dimen/closeSize"
android:layout_height="@dimen/closeSize"
android:layout_alignParentEnd="true"
android:layout_margin="24dp"
android:src="@mipmap/btn_close" />
</RelativeLayout>
<!--主体-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingHorizontal="@dimen/dp_20">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="会议室名称:"
android:textColor="@color/ThemeDarkPurple"
android:textSize="@dimen/default.title"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:textStyle="bold"
android:id="@+id/dialog_champer_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="植物园"
android:textColor="@color/ThemeDarkPurple"
android:textSize="@dimen/default.title"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="10dp">
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主题:"
android:textColor="@color/ThemeDarkPurple"
android:textSize="@dimen/default.title"
tools:ignore="HardcodedText" />
<TextView
android:textStyle="bold"
android:id="@+id/dialog_champer_theme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="张峰发起的会议"
android:textColor="@color/ThemeDarkPurple"
android:textSize="@dimen/default.title"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="10dp">
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时间:"
android:textColor="@color/ThemeDarkPurple"
android:textSize="@dimen/default.title"
tools:ignore="HardcodedText" />
<TextView
android:textStyle="bold"
android:id="@+id/dialog_champer_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="22:00-23:00"
android:textColor="@color/ThemeDarkPurple"
android:textSize="@dimen/default.title"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="10dp">
<TextView
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发起人:"
android:textColor="@color/ThemeDarkPurple"
android:textSize="@dimen/default.title"
tools:ignore="HardcodedText" />
<TextView
android:textStyle="bold"
android:id="@+id/dialog_champer_host"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="张峰"
android:textColor="@color/ThemeDarkPurple"
android:textSize="@dimen/default.title"
tools:ignore="HardcodedText" />
</RelativeLayout>
</LinearLayout>
<!--底部-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingVertical="20dp">
<ImageView
android:id="@+id/dialog_champer_qr"
android:layout_width="160dp"
android:layout_height="160dp"
android:src="@mipmap/ic_launcher"
android:layout_marginBottom="6dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请扫码完善会议信息"
tools:ignore="HardcodedText"
android:textSize="@dimen/tip"
android:textColor="#AD070E4F"
android:layout_marginBottom="4dp" />
<TextView
android:id="@+id/dialog_champer_countdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="15s"
android:textColor="@color/ThemeBlue"
android:textStyle="bold"
android:textSize="22sp"
tools:ignore="HardcodedText"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
调用
val builder = CountDownDialog.Builder(this)
builder.apply {
setTitle("1")
setContent("2")
setTheme("3")
setTime("4")
setHost("5")
setQRImage(resources.getDrawable(R.mipmap.default_person))
setPositiveButton(DialogInterface.OnClickListener { dialog, _ ->
dialog.dismiss()
})
create().show()
}
|