之前总是遇到弹窗Dialog,为了方便以后使用,自己方便简单封装了一个Utils方便以后根据需求方便拓展应用。截图如下:
废话不多说,直接上代码
1.CommonDialog.kt
···
package com.util.dialog
import android.app.Dialog
import android.content.Context
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.rey.material.widget.Button
/**
- 作者:yshr on 2018/1/31 19:44
*/
class CommonDialog : Dialog {
// var ImageView ivDialogCancel? = null;
var tvTitle: TextView? = null
var ivDialogIcon: ImageView? = null
var tvDialogContent: TextView? = null
var btDialogConfirm: Button? = null //确定按钮可通过外部自定义按钮内容
var tvDialogCancel: TextView? = null //取消
constructor(context: Context) : super(context) {
initView()
}
constructor(context: Context, themeStyle: Int) : super(context, themeStyle) {
initView()
}
private fun initView() {
setContentView(R.layout.dialog_common)
setCanceledOnTouchOutside(false)
// ivDialogCancel = findViewById(R.id.iv_dialog_cancel);
tvTitle = findViewById(R.id.tv_dialog_title)
ivDialogIcon = findViewById(R.id.iv_dialog_icon)
tvDialogContent = findViewById(R.id.tv_dialog_content)
btDialogConfirm = findViewById<View>(R.id.bt_dialog_confirm) as com.rey.material.widget.Button
tvDialogCancel = findViewById<View>(R.id.tv_dialog_cancel) as com.tengniu.p2p.tnp2p.view.MyRoundTextView
}
class Builder(val context: Context) {
var confirmListener: OnConfirmListener? = null
var cancelListener: OnCancelListener? = null
var title: String? = null
var icon: Int? = 0
var content: String? = null
var btConfirmText: String? = null
var tvCancelText: String? = null
var cancelIsVisibility: Boolean? = true
fun setOnConfirmListener(confirmListener: OnConfirmListener): Builder {
this.confirmListener = confirmListener
return this
}
fun setOnCancelListener(cancelListener: OnCancelListener): Builder {
this.cancelListener = cancelListener
return this
}
fun setTitle(title: String): Builder {
this.title = title
return this
}
fun setIcon(icon: Int): Builder {
this.icon = icon
return this
}
fun setContent(content: String): Builder {
this.content = content
return this
}
// 点击确定按钮的文字
fun setConfirmText(btConfirmText: String): Builder {
this.btConfirmText = btConfirmText
return this
}
//取消按钮的文字
fun setCancelText(tvCancelText: String): Builder {
this.tvCancelText = tvCancelText
return this
}
fun setCancelIconIsVisibility(cancelIsVisibility: Boolean): Builder {
this.cancelIsVisibility = cancelIsVisibility
return this
}
fun create(): CommonDialog {
val dialog = CommonDialog(context, R.style.custom_dialog2)
if (!TextUtils.isEmpty(title)) {
dialog.tvTitle?.text = this.title
} else {
dialog.tvTitle?.visibility = View.GONE
}
dialog.tvDialogContent?.text = this.content
if (icon != 0) {
dialog.ivDialogIcon?.setImageResource(this.icon!!)
}
dialog.btDialogConfirm?.text = this.btConfirmText ?: "确认"
if (this.cancelIsVisibility!!) {
dialog.tvDialogCancel?.text = this.tvCancelText ?: "取消"
} else {
dialog.tvDialogCancel?.visibility = View.GONE
}
if (cancelListener != null) {
dialog.tvDialogCancel?.setOnClickListener { v -> cancelListener!!.onClick(dialog) }
}
if (confirmListener != null) {
dialog.btDialogConfirm?.setOnClickListener { v -> confirmListener!!.onClick(dialog) }
}
return dialog
}
}
// 点击弹窗取消按钮回调
interface OnCancelListener {
fun onClick(dialog: Dialog)
}
// 点击弹窗跳转回调
interface OnConfirmListener {
fun onClick(dialog: Dialog)
}
}
···
-
dialog布局 R.layout.dialog_common
···
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_corners_white"
android:paddingBottom="@dimen/screen_margins_35"
android:paddingLeft="@dimen/screen_margins_10"
android:paddingRight="@dimen/screen_margins_10"><ImageView
android:id="@+id/iv_dialog_cancel"
android:layout_width="30dp"
android:layout_height="20dp"
android:layout_margin="10dp"
android:src="@mipmap/ic_dialog_cancel"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" /><TextView
android:id="@+id/tv_dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示"
android:textColor="@color/black_1"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_dialog_cancel" /><ImageView
android:id="@+id/iv_dialog_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_dialog_title" /><TextView
android:id="@+id/tv_dialog_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="根据自己不同的需求,可自定义此弹窗的样式布局,添加Icon布局以及底部按钮的个数,外部调用方式也将在样例中展示。"
android:textColor="@color/grey_15"
app:layout_constraintTop_toBottomOf="@id/iv_dialog_icon" /><android.support.constraint.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/screen_margins_25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_dialog_content"><com.tengniu.p2p.tnp2p.view.MyRoundTextView android:id="@+id/tv_dialog_cancel" android:layout_width="100dp" android:layout_height="38dp" android:gravity="center" android:text="取消" android:textColor="@color/orange_7" android:textSize="15sp" app:radius="30dp" app:strokeColor="@color/orange_7" app:strokeWidth="0.5dp" /> <com.rey.material.widget.Button android:id="@+id/bt_dialog_confirm" style="@style/Common_Button" android:layout_width="100dp" android:layout_height="38dp" android:text="确认" android:textSize="15sp" android:layout_marginLeft="@dimen/screen_margins_15" app:layout_goneMarginStart="0dp" app:layout_constraintStart_toEndOf="@id/tv_dialog_cancel" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
···
3.外部调用方式如下简单粗暴
CommonDialog.Builder(context)
.setTitle("用户提示") .setContent(userModel.userRiskEvaluationVO.showNeedEvaluationContent)
.setIcon(R.drawable.ic_risk)
.setConfirmText("确认")
.setCancelText("取消")
.setOnConfirmListener(object : CommonDialog.OnConfirmListener {
override fun onClick(dialog: Dialog) {
...
...
dialog.dismiss()
}
})
.setOnCancelListener(object : CommonDialog.OnCancelListener {
override fun onClick(dialog: Dialog) {
dialog.dismiss()
}
})
.create()
.show()
以上便是简单的dialog封装,以方便大家以后直接使用。