案例場景
當(dāng)接口的方法過多章贞,而我們只需要其中一個方法時,例如TextView.addTextChangedListener袋坑,我們只需要它的onTextChanged方法時
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")
}
})
顯然,我們這里還需要空實現(xiàn)afterTextChanged和beforeTextChanged,有點繁瑣
2. 空適配器模式
tv.addTextChangedListener(object : EmptyTextWatcher() {
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
print("文字修改為:$s")
}
})
這個其實很簡單添寺,就是加了個接口空實現(xiàn)的中間類而已,這也是我們通常的操作懈费,但是在kotlin里面计露,我們還可以有下面這兩種高級玩法
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")
}
)
這個就是加了一個內(nèi)聯(lián)的拓展方法addTextChangedListenerClosure,這個方法的參數(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")
}
}
這個呢楞捂,就是先通過拓展方法傳入方法體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的一個簡單示例寨闹,主要是示范怎么寫DSL,如果你有更好的使用DSL的場景君账,歡迎補充繁堡。