6

我在下面有一些扩展功能。

fun EditText.setEmailValidationListener(): TextWatcher {
    val textWatcher = object : TextWatcher {
        override fun beforeTextChanged(text: CharSequence?, start: Int, count: Int, after: Int) { }
        override fun onTextChanged(text: CharSequence?, start: Int, before: Int, count: Int) { }
        override fun afterTextChanged(text: Editable?) { validateEmail() }

        private fun validateEmail(): Boolean {
            if (validateEmailFormat(showError = false)) {
                getParentInputLayout()?.isErrorEnabled = false
                return true
            }
            return false
        }
    }
    addTextChangedListener(textWatcher)

    return textWatcher

}

fun EditText.setPasswordValidationListener(): TextWatcher {
    val textWatcher = object : TextWatcher {
        override fun beforeTextChanged(text: CharSequence?, start: Int, count: Int, after: Int) { }
        override fun onTextChanged(text: CharSequence?, start: Int, before: Int, count: Int) { }
        override fun afterTextChanged(text: Editable?) { validateEmpty() }

        private fun validatePasswordText(): Boolean {
            if (validateEmptyText(showError = false)) {
                getParentInputLayout()?.isErrorEnabled = false
                return true
            }
            return false
        }
    }

    addTextChangedListener(textWatcher)

    return textWatcher
}

fun EditText.validateEmailFormat(showError: Boolean = true): Boolean 
{
    // Do something checking the Email
    return false
}

fun EditText.validatePasswordText(showError: Boolean = true): Boolean     
{
    // Do something checking the Password
    return false
}

private fun EditText.getParentInputLayout(): TextInputLayout? {
    if (parent is TextInputLayout) {
        return parent as TextInputLayout
    }
    return null
}

两者setEmailValidationListenersetPasswordValidationListener是相同的,除了它们分别使用的验证功能,即validateEmailFormatvalidatePasswordFormat

所以我打算将这两个函数的通用代码重构为一个通用函数如下

fun EditText.setupTextChangeListener(validatorFunc : (showError: Boolean) -> Boolean): TextWatcher {
    val textWatcher = object : TextWatcher {
        override fun beforeTextChanged(text: CharSequence?, start: Int, count: Int, after: Int) { }
        override fun onTextChanged(text: CharSequence?, start: Int, before: Int, count: Int) { }
        override fun afterTextChanged(text: Editable?) { validateEmpty() }

        private fun validateEmpty(): Boolean {
            if (validatorFunc(false)) {
                getParentInputLayout()?.isErrorEnabled = false
                return true
            }
            return false
        }
    }

    addTextChangedListener(textWatcher)

    return textWatcher
}

...它基本上只是validationFunc作为参数发送给它。

但是,我找不到将EditText.validateEmailFormatand发送EditText.validatePasswordFormatvalidationFunc函数参数的任何方法。

我怎么能做到这一点?

4

2 回答 2

8

一些理论

扩展函数的签名比最初看起来要复杂一些。扩展需要对此类的对象进行一些引用才能对其进行操作。

实际上扩展方法

fun EditText.validateEmailFormat(showError: Boolean = true): Boolean

反编译为普通的旧 java 后,如下所示:

public static final boolean validateEmailFormat(@NotNull EditText $receiver, boolean showError)

因为(几乎)不可能更改已经编译的 Java 类。所以 Kotlin(很可能还有其他具有扩展方法概念的语言)使用静态方法,第一个参数是扩展类的接收者,以使其工作。

回到业务

validateEmailFormat实际上是 type EditText.(Boolean) -> Boolean,同时也是 type (EditText, Boolean) -> Boolean。所以你需要做两件事之一:

首先,您可以将EditText.setupTextChangeListener接受validatorFunc作为 EditText.(Boolean) -> Boolean(EditText, Boolean) -> Boolean代替(Boolean) -> Boolean

或者你避免扩展EditTextfun EditText.validateEmailFormat(Boolean)使其成为普通的 Kotlin 函数,例如这样的东西fun validateEmailFormat(String, Boolean)

由于您广泛使用扩展功能,我认为第一个选项对您来说是正确的解决方案。

于 2016-08-19T06:20:09.713 回答
0

fun EditText.validateEmailFormat()可以作为EditText::validateEmailFormat.

于 2022-03-03T10:27:04.240 回答