0

如果 2 编辑文本是填充使用,如何启用按钮'androidx.core:core-ktx:1.1.0'

override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        editTextEmailOrPhone.doOnTextChanged { emailOrPhone, _, _, _ ->
            if (isValidEmailOrPhone(emailOrPhone.toString())) {
                buttonLogin.isEnabled = true
            } else {
                edtPassword.doOnTextChanged { password, _, _, _ ->
                buttonLogin.isEnabled = password.toString().length >= 8
                }
            }
        }
    }

    private fun isValidEmailOrPhone(text: String): Boolean =
        android.util.Patterns.EMAIL_ADDRESS.matcher(text).matches() || android.util.Patterns.PHONE.matcher(
            text
        ).matches()

我试过了。如果我填写电子邮件(启用按钮)<== 看起来不错

但是,如果我删除电子邮件(按钮未禁用)<== 问题

4

1 回答 1

1
edtPassword.doOnTextChanged { password, _, _, _ ->
    onInfoChange(password?.toString(), editTextEmailOrPhone.text?.toString())
}
editTextEmailOrPhone.doOnTextChanged { emailOrPhone, _, _, _ ->
    onInfoChange(edtPassword.text?.toString(), emailOrPhone?.toString())
}
private fun onInfoChange(password : String?, emailOrPhone : String?){
    buttonLogin.isEnabled = isValidEmailOrPhone(emailOrPhone) && password?.length ?: 0 >= 8
}
于 2020-02-07T17:07:36.253 回答