自定义弹框的简单写法Demo

效果

【Android,Kotlin】自定义弹框的简单写法Demo_xml

源码

​Koltin​​ CustomDialog

package com.sq.module.widget

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.sq.module.R


/**
* @ClassName ExitDialog
* @Description TODO
* @Author Kolin Zhao
* @Date 2021/6/3 20:45
* @Version 1.0
*/
class CustomDialog : Dialog {
constructor(context: Context?) : super(context!!)
constructor(context: Context?, theme: Int) : super(context!!, theme)

class Builder(private val context: Context) {
var content: String? = null
private var positiveButtonClickListener: DialogInterface.OnClickListener? = null
private var negativeButtonClickListener: DialogInterface.OnClickListener? = null

fun setContent(content: String): Builder {
this.content = content
return this
}

fun setPositiveButton(listener: DialogInterface.OnClickListener): Builder {
this.positiveButtonClickListener = listener
return this
}

fun setNegativeButton(listener: DialogInterface.OnClickListener): Builder {
this.negativeButtonClickListener = listener
return this
}

fun create(): CustomDialog {
val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
//为自定义弹框设置主题
val customDialog = CustomDialog(context, R.style.CustomDialog)
val view = layoutInflater.inflate(R.layout.dialog_exit, null)
customDialog.addContentView(view, LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
))
//设置弹框内容
content?.let {
(view.findViewById(R.id.dialog_content) as TextView).text = it
}
//设置弹框按钮
positiveButtonClickListener?.let {
(view.findViewById(R.id.dialog_sure) as Button).setOnClickListener {
positiveButtonClickListener!!.onClick(customDialog, DialogInterface.BUTTON_POSITIVE)
}
} ?: run {
(view.findViewById(R.id.dialog_sure) as Button).visibility = View.GONE
}
negativeButtonClickListener?.let {
(view.findViewById(R.id.dialog_cancel) as Button).setOnClickListener {
negativeButtonClickListener!!.onClick(customDialog, DialogInterface.BUTTON_NEGATIVE)
}
} ?: run {
(view.findViewById(R.id.dialog_cancel) as Button).visibility = View.GONE
}
customDialog.setContentView(view)
return customDialog
}
}
}

调用

val builder = CustomDialog.Builder(this)
builder.setPositiveButton(DialogInterface.OnClickListener { dialogInterface, _ ->
dialogInterface.dismiss()
//具体逻辑
})
builder.setNegativeButton(DialogInterface.OnClickListener { dialogInterface, _ ->
dialogInterface.dismiss()
//具体逻辑
})
builder.create().show()

​Layout​​ dialog_exit.xml

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="@dimen/marginDialog"
android:background="@drawable/champer_form"
android:orientation="vertical"
android:padding="20dp">

<TextView
android:id="@+id/dialog_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="36dp"
android:text="确定要退出账号?"
tools:ignore="HardcodedText"
android:textColor="@color/ThemeDarkPurple"
android:textSize="@dimen/title"
android:layout_gravity="center"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/dialog_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="6dp"
android:layout_weight="1"
android:background="@drawable/exit_cancel"
android:text="取 消"
android:textColor="@color/ThemeBlue"
android:textSize="20sp"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/dialog_sure"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/exit_sure"
android:text="确定"
android:textColor="@color/white"
android:textSize="20sp"
tools:ignore="HardcodedText"
android:layout_marginStart="6dp"/>
</LinearLayout>

</LinearLayout>

</FrameLayout>

champer_form.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"/>
<corners android:radius="6dp"/>
</shape>

styles.xml

<!--退出自定义弹框-->
<style name="CustomDialog" parent="Theme.AppCompat.Dialog">
<item name="android:background">@color/transparent</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>