@[toc]
當(dāng)接口的方法過多,而我們只需要其中一個(gè)方法時(shí),例如TextView.addTextChangedListener,我們只需要它的onTextChanged方法時(shí)
1.原始方式
tv.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
print("文字修改為:$s")
}
})
顯然,我們這里還需要空實(shí)現(xiàn)afterTextChanged和beforeTextChanged,有點(diǎn)繁瑣
2. 空適配器模式
tv.addTextChangedListener(object : EmptyTextWatcher() {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
print("文字修改為:$s")
}
})
這個(gè)其實(shí)很簡(jiǎn)單,就是加了個(gè)接口空實(shí)現(xiàn)的中間類而已,這也是我們通常的操作,但是在kotlin里面,我們還可以有下面這兩種高級(jí)玩法
open class EmptyTextWatcher : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
}
3. 高階函數(shù)方式
tv.addTextChangedListenerClosure(
onTextChanged = { charSequence, start, after, count ->
print("文字修改為:$charSequence")
}
)
這個(gè)就是加了一個(gè)內(nèi)聯(lián)的拓展方法addTextChangedListenerClosure,這個(gè)方法的參數(shù)都是函數(shù)
inline fun TextView.addTextChangedListenerClosure(
crossinline afterTextChanged: (Editable?) -> Unit = {},
crossinline beforeTextChanged: (CharSequence?, Int, Int, Int) -> Unit = { charSequence, start, count, after -> },
crossinline onTextChanged: (CharSequence?, Int, Int, Int) -> Unit = { charSequence, start, after, count -> }
) {
val listener = object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
afterTextChanged.invoke(s)
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
beforeTextChanged.invoke(s, start, count, after)
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
onTextChanged.invoke(s, start, before, count)
}
}
this.addTextChangedListener(listener)
}
4. DSL方式
tv.addTextChangedListenerDsl {
onTextChanged { charSequence, start, after, count ->
print("文字修改為:$charSequence")
}
}
這個(gè)呢,就是先通過拓展方法傳入方法體init,在init方法里面,初始化afterTextChanged等函數(shù),然后在回調(diào)的方法里面調(diào)用保存的函數(shù)
fun TextView.addTextChangedListenerDsl(init: TextChangedListenerDsl.() -> Unit) {
val listener = TextChangedListenerDsl()
listener.init()
this.addTextChangedListener(listener)
}
class TextChangedListenerDsl : TextWatcher {
var afterTextChanged: ((Editable?) -> Unit)? = null
var beforeTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null
var onTextChanged: ((CharSequence?, Int, Int, Int) -> Unit)? = null
/**
* DSL方法
*/
fun afterTextChanged(method: (Editable?) -> Unit) {
afterTextChanged = method
}
fun beforeTextChanged(method: (CharSequence?, Int, Int, Int) -> Unit) {
beforeTextChanged = method
}
fun onTextChanged(method: (CharSequence?, Int, Int, Int) -> Unit) {
onTextChanged = method
}
/**
* 原始方法
*/
override fun afterTextChanged(s: Editable?) {
afterTextChanged?.invoke(s)
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
beforeTextChanged?.invoke(s, start, count, after)
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
onTextChanged?.invoke(s, start, before, count)
}
}
以上只是DSL的一個(gè)簡(jiǎn)單示例,主要是示范怎么寫DSL,如果你有更好的使用DSL的場(chǎng)景,歡迎補(bǔ)充。