1 Star 0 Fork 0

yaozhukuang/widget

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
DialogBuilder.kt 3.92 KB
Copy Edit Raw Blame History
yaozhukuang authored 2018-07-11 14:13 . Upload DialogBuilder.kt
package com.zw.yzk.component.common.dialog
import android.content.Context
import android.graphics.Point
import android.widget.TextView
import android.support.v7.app.AlertDialog
import android.view.*
/**
* 示例用法
* DialogBuilder.Builder(this, R.layout.dialog_select_image)
* .setAnimation(R.style.bottom_animation)
* .addItem(DialogBuilder.Item(R.id.album, getString(R.string.album), View.OnClickListener {
* getCameraPermission()
* }))
* .addItem(DialogBuilder.Item(R.id.cancel, getString(R.string.cancel)))
* .setSize(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
* .setGravity(Gravity.BOTTOM)
* .setCancelable(true)
* .build().show()
*/
class DialogBuilder {
class Item(id: Int, text: String? = null, listener: View.OnClickListener? = null) {
val itemId: Int = id
val itemText: String? = text
val clickListener: View.OnClickListener? = listener
}
//对话框属性设置
private class AlertParams(var context: Context, var view: View) {
val itemList: MutableList<Item> = mutableListOf()
//是否可点击区域外取消
var cancelable: Boolean = false
//动画
var animationId: Int = 0
//显示位置
var gravity: Int = Gravity.NO_GRAVITY
//设置对话框style
var style: Int = 0
//设置大小
var size: Point? = null
}
class Builder(context: Context, view: View) {
private val params: AlertParams = AlertParams(context, view)
constructor(context: Context, layoutId: Int) : this(context, LayoutInflater.from(context).inflate(layoutId, null))
fun addItem(item: Item): Builder {
params.itemList.add(item)
return this
}
fun setCancelable(cancelable: Boolean): Builder {
params.cancelable = cancelable
return this
}
fun setAnimation(animation: Int): Builder {
params.animationId = animation
return this
}
fun setGravity(gravity: Int): Builder {
params.gravity = gravity
return this
}
fun setStyle(style: Int): Builder {
params.style = style
return this
}
fun setSize(width: Int, height: Int): Builder {
params.size = Point(width, height)
return this
}
fun build(): AlertDialog {
val dialog = AlertDialog.Builder(params.context, params.style)
.setView(params.view).setCancelable(params.cancelable).create()
//设置背景透明
val window = dialog.window
window?.let {
window.setBackgroundDrawableResource(android.R.color.transparent)
window.decorView.setPadding(0, 0, 0, 0)
window.attributes.width = ViewGroup.LayoutParams.WRAP_CONTENT
window.attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT
params.size?.let {
window.attributes.width = params.size!!.x
window.attributes.height = params.size!!.y
}
//设置展示位置
window.attributes.gravity = params.gravity
//设置动画
window.setWindowAnimations(params.animationId)
}
//设置item
params.itemList.forEach { item ->
params.view.findViewById<View>(item.itemId)?.apply {
setOnClickListener {
item.clickListener?.onClick(it)
dialog.dismiss()
}
item.itemText?.let {
(this as TextView).text = item.itemText
}
}
}
return dialog
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yaozhukuang/widget.git
git@gitee.com:yaozhukuang/widget.git
yaozhukuang
widget
widget
master

Search