后来又开始写安卓,士别三日党刮目相看,我这是别了大概两年多吧,kotlin已经不再新鲜,IO大会出现了很多新特性,Instant run,Android Q bla bla不管他,不管被拉下多远还是要操练起来。

现状是目前的安卓项目中已经集成了kotlin的一个插件,信息提示的,比较炫酷,长这个样子 => 传送门

好吧,放了两天都不知道要说什么了,只记得最后加了个onShow和onHide的回调,用来满足业务需求,例如请求api发现客户端token无效或超时而要求用户重新登录,封装了一个Util,直接贴代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Created by Lison on 7/9/2019.
*
* http://lison.cc
*/
class AlerterUtil(
val activity: Activity,
val title: String,
val titleId: Int,
val text: String,
val textId: Int,
val backgroundColor: Int,
val icon: Int,
val showAlertListener: OnShowAlertListener,
val hideAlertListener: OnHideAlertListener
) {
private constructor(builder: Builder) : this(builder.activity, builder.title, builder.titleId, builder.text, builder.textId, builder.backgroundColor, builder.icon, builder.showAlertListener, builder.hideAlertListener)

class Builder {

/** lateinit 可以只創建變量不賦值 **/
lateinit var activity: Activity
private set
var title: String = ""
private set
var titleId: Int = 0
private set
var text: String = ""
private set
var textId: Int = 0
private set
var backgroundColor: Int = 0
private set
var icon: Int = 0
private set

var showAlertListener: OnShowAlertListener = OnShowAlertListener { }
private set
var hideAlertListener: OnHideAlertListener = OnHideAlertListener { }
private set

constructor(activity: Activity) {
this.activity = activity
}

fun title(title: String) = apply {
this.title = title
}

fun title(titleId: Int) = apply { this.titleId = titleId }

fun text(text: String) = apply {
this.text = text
}

fun text(textId: Int) = apply { this.textId = textId }

fun backGroundColor(color: Int) = apply {
this.backgroundColor = color
}

fun icon(icon: Int) = apply {
this.icon = icon
}

fun onShowAlertListener(onShowAlertListener: OnShowAlertListener) = apply {
this.showAlertListener = onShowAlertListener
}

fun onHideAlertListener(onHideAlertListener: OnHideAlertListener) = apply {
this.hideAlertListener = onHideAlertListener
}

fun build() = AlerterUtil(this)

fun ok() = apply {
backgroundColor = R.color.colorSpring
icon = R.drawable.icon_ok
}

fun warn() = apply {
backgroundColor = R.color.colorAlert
}

fun error() = apply {
backgroundColor = R.color.colorError
icon = R.drawable.icon_error
}
}

fun alert() {
var al = Alerter.create(activity)
if (TextUtil.isNotBlank(title))
al.setTitle(title)
else if (titleId != 0)
al.setTitle(titleId)

if (TextUtil.isNotBlank(text))
al.setText(text)
else if (textId != 0)
al.setText(textId)

if (backgroundColor != 0)
al.setBackgroundColorRes(backgroundColor)
else
al.setBackgroundColorRes(R.color.colorPrimary)
if (icon != 0)
al.setIcon(icon)

if (showAlertListener != null)
al.setOnShowListener(showAlertListener)
if (hideAlertListener != null)
al.setOnHideListener(hideAlertListener)

al.show()
}

调用方式如下

1
2
3
4
5
6
new AlerterUtil.Builder(this).text(getString(R.string.need_barcode)).onHideAlertListener(new OnHideAlertListener() {
@Override
public void onHide() {
//打开登录Activity
}
}).build().alert();

kotlin是个好东西,与java无缝衔接;设计模式更是把好武器。好了好了困了。

参考资料: https://www.cnblogs.com/scuwangjun/p/9699895.html https://github.com/Tapadoo/Alerter https://stackoverflow.com/questions/36140791/how-to-implement-builder-pattern-in-kotlin