kotlin使用建造者模式自定义对话框

本文实例为大家分享了kotlin自定义对话框的具体代码,供大家参考,具体内容如下

1.CommonDialog 创建我们自己的对话框,继承于系统的Dialog 实现构造方法

class CommonDialog(context: Context?, themeResId: Int) : Dialog(context, themeResId) {}

\2. 在内部创建BUilder类 定义出我们需要的方法和属性

class Builder (private val context: Context) {
private var title: String? = null
private var message: String? = null
private var positiveButtonContent: String? = null
private var negativeButtonContent: String? = null
private var positiveButtonListener: DialogInterface.OnClickListener? = null
private var negativeButtonListener: DialogInterface.OnClickListener? = null
private var contentView: View? = null
private var imageid: Int = 0
private var color: Int = 0
private var withOffSize: Float = 0.toFloat()
private var heightOffSize: Float = 0.toFloat()
fun setTitle(title: String): Builder {
this.title = title
return this
}
fun setTitle(title: Int): Builder {
this.title = context.getText(title) as String
return this
}
fun setMessage(message: String): Builder {
this.message = message
return this
}
fun setMessageColor(color: Int): Builder {
this.color = color
return this
}
fun setImageHeader(Imageid: Int): Builder {
this.imageid = Imageid
return this
}
fun setPositiveButton(text: String, listener: DialogInterface.OnClickListener): Builder {
this.positiveButtonContent = text
this.positiveButtonListener = listener
return this
}
fun setPositiveButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {
this.positiveButtonContent = context.getText(textId) as String
this.positiveButtonListener = listener
return this
}
fun setNegativeButton(text: String, listener: DialogInterface.OnClickListener): Builder {
this.negativeButtonContent = text
this.negativeButtonListener = listener
return this
}
fun setNegativeButton(textId: Int, listener: DialogInterface.OnClickListener): Builder {
this.negativeButtonContent = context.getText(textId) as String
this.negativeButtonListener = listener
return this
}
fun setContentView(v: View): Builder {
this.contentView = v
return this
}
fun setWith(v: Float): Builder {
this.withOffSize = v
return this
}
fun setContentView(v: Float): Builder {
this.heightOffSize = v
return this
}
fun create(): CommonDialog {
/**
* 利用我们刚才自定义的样式初始化Dialog
*/
val dialog = CommonDialog(context,
R.style.dialogStyle)
/**
* 下面就初始化Dialog的布局页面
*/
val inflater = context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val dialogLayoutView = inflater.inflate(R.layout.dialog_layout,
null)
dialog.addContentView(dialogLayoutView, ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT))
if (imageid != 0) {
(dialogLayoutView.findViewById<View (R.id.iv_image_header) as ImageView)
.setImageResource(imageid)
} else {
(dialogLayoutView.findViewById<View (R.id.iv_image_header) as ImageView).visibility = View.GONE
}
if (!TextUtils.isEmpty(title)) {
(dialogLayoutView.findViewById<View (R.id.tv_dialog_title) as TextView).text = title
} else {
// Log.w(context.getClass().toString(), "未设置对话框标题!");
}
if (color != 0) {
val viewById = dialogLayoutView.findViewById<View (R.id.dialog_content) as TextView
viewById.setTextColor(color)
}
if (!TextUtils.isEmpty(message)) {
(dialogLayoutView.findViewById<View (R.id.dialog_content) as TextView).text = message
} else if (contentView != null) {
(dialogLayoutView
.findViewById<View (R.id.dialog_llyout_content) as LinearLayout)
.removeAllViews()
(dialogLayoutView
.findViewById<View (R.id.dialog_llyout_content) as LinearLayout).addView(
contentView, ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT))
} else {
(dialogLayoutView.findViewById<View (R.id.dialog_content) as TextView).visibility = View.INVISIBLE
}
if (!TextUtils.isEmpty(positiveButtonContent)) {
(dialogLayoutView.findViewById<View (R.id.tv_dialog_pos) as TextView).text = positiveButtonContent
if (positiveButtonListener != null) {
(dialog.findViewById<View (R.id.tv_dialog_pos) as TextView)
.setOnClickListener { positiveButtonListener!!.onClick(dialog, -1) }
}
} else {
(dialogLayoutView.findViewById<View (R.id.tv_dialog_pos) as TextView).visibility = View.GONE
dialogLayoutView.findViewById<View (R.id.line).visibility = View.GONE
}
if (!TextUtils.isEmpty(negativeButtonContent)) {
(dialogLayoutView.findViewById<View (R.id.tv_dialog_neg) as TextView).text = negativeButtonContent
if (negativeButtonListener != null) {
(dialogLayoutView
.findViewById<View (R.id.tv_dialog_neg) as TextView)
.setOnClickListener { negativeButtonListener!!.onClick(dialog, -2) }
}
} else {
(dialogLayoutView.findViewById<View (R.id.tv_dialog_neg) as TextView).visibility = View.GONE
}
/**
* 将初始化完整的布局添加到dialog中
*/
dialog.setContentView(dialogLayoutView)
/**
* 禁止点击Dialog以外的区域时Dialog消失
*/
dialog.setCanceledOnTouchOutside(false)
val window = dialog.window
val context = this.context as Activity
val windowManager = context.windowManager
val defaultDisplay = windowManager.defaultDisplay
val attributes = window!!.attributes
if (withOffSize.toDouble() != 0.0) {
attributes.width = (defaultDisplay.width * withOffSize).toInt()
} else {
attributes.width = (defaultDisplay.width * 0.77).toInt()
}
if (heightOffSize.toDouble() != 0.0) {
attributes.height = (defaultDisplay.height * heightOffSize).toInt()
}
window.attributes = attributes
return dialog
}
}

3.在需要的地方使用

CommonDialog.Builder(this).
setImageHeader(R.mipmap.icon_gantan_tankuang)
.setTitle("你是否要注销账户")
.setMessage("注销后需重新注册才能使用牛返返优惠")
.setPositiveButton("确定注销", DialogInterface.OnClickListener { p0, p1 - 
p0?.dismiss()
DestroyAccount()
})
.setNegativeButton("取消", DialogInterface.OnClickListener { p0, p1 -  p0?.dismiss() })
.setWith(0.77f)
.create()
.show()

实现效果:

img

以上就是本文的全部内容,希望对大家的学习有所帮助。

更多Android进阶指南 可以扫码 解锁 《Android十大板块文档》

1.Android车载应用开发系统学习指南(附项目实战)

2.Android Framework学习指南,助力成为系统级开发高手

3.2024最新Android中高级面试题汇总+解析,告别零offer

4.企业级Android音视频开发学习路线+项目实战(附源码)

5.Android Jetpack从入门到精通,构建高质量UI界面

6.Flutter技术解析与实战,跨平台首要之选

7.Kotlin从入门到实战,全方面提升架构基础

8.高级Android插件化与组件化(含实战教程和源码)

9.Android 性能优化实战+360°全方面性能调优

10.Android零基础入门到精通,高手进阶之路

敲代码不易,关注一下吧。ღ( ´・ᴗ・` ) 🤔

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Kotlin使用默认样式或自定义样式对话框,可以按照以下步骤进行操作: 1. 默认样式: - 在布局文件中定义对话框的内容。创建一个名为`custom_dialog_layout.xml`的布局文件,并在其中定义对话框的内容。 - 在代码中创建对话框实例并设置内容布局。 - 调用`dialog.show()`方法显示对话框。 ```kotlin val dialog = Dialog(context) dialog.setContentView(R.layout.custom_dialog_layout) dialog.show() ``` 2. 自定义样式: - 在`styles.xml`文件中定义自定义样式。创建一个名为`CustomDialogStyle`的样式,并在其中设置对话框的背景、边框等属性。 ```xml <style name="CustomDialogStyle" parent="Theme.AppCompat.Dialog"> <item name="android:windowBackground">@drawable/custom_dialog_background</item> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@style/CustomDialogAnimation</item> </style> ``` - 创建一个名为`custom_dialog_background.xml`的drawable文件,用于设置对话框的背景。 - 在布局文件中定义对话框的内容。 - 在代码中创建对话框实例并设置样式和内容布局。 - 调用`dialog.show()`方法显示对话框。 ```kotlin val dialog = Dialog(context, R.style.CustomDialogStyle) dialog.setContentView(R.layout.custom_dialog_layout) dialog.show() ``` 请注意,这只是简单的示例,您可以根据自己的需求进行更多的定制。希望这些信息对您有所帮助!如果有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值