有没有办法避免重复类似的 TextInputEditText 验证?
Is there a way to avoid repetition with similiar TextInputEditText validation?
片段中有 5 个 TextInputEditText 字段。第一个是字符串,另外 4 个是用户必须输入的双精度值。
为确保值有效,检查每个字段是否为空,并检查最后四个(带双精度值)是否为双精度值。
在下面的代码片段中,我删除了最后 2 个 val 和 fun 声明,因为它们与最后 2 个 oes 完全相同,除了 TextInPutLayout 名称(和相应的 val)。
所以,我想知道是否有可能以任何方式缩短它
private val enterTextFoodName = view.findViewById<TextInputLayout>(R.id.enter_food_name)
private val enterTextKcal = view.findViewById<TextInputLayout>(R.id.enter_kcal)
private val enterTextCarbs = view.findViewById<TextInputLayout>(R.id.enter_carbs)
[...]
private fun validateValues(): Boolean {
return (!validateFoodName()
|| !validateKcal()
|| !validateCarbs()
[...])
}
private fun validateFoodName(): Boolean {
return when {
enterTextFoodName.editText?.text.toString().trim().isEmpty() -> {
enterTextFoodName.error = getString(R.string.cant_be_empty)
false
}
else -> {
enterTextFoodName.error = null
true
}
}
}
private fun validateKcal(): Boolean {
return when {
enterTextKcal.editText?.text.toString().trim().isEmpty() -> {
enterTextKcal.error = getString(R.string.cant_be_empty)
false
}
enterTextKcal.editText?.text.toString().trim().toDoubleOrNull() == null -> {
enterTextKcal.error = getString(R.string.invalid_value)
false
}
else -> {
enterTextKcal.error = null
true
}
}
}
private fun validateCarbs(): Boolean {
return when {
enterTextCarbs.editText?.text.toString().trim().isEmpty() -> {
enterTextCarbs.error = getString(R.string.cant_be_empty)
false
}
enterTextCarbs.editText?.text.toString().trim().toDoubleOrNull() == null -> {
enterTextCarbs.error = getString(R.string.invalid_value)
false
}
else -> {
enterTextCarbs.error = null
true
}
}
}
[...]
可以通过Kotlin中的扩展函数实现:
inline fun TextInputLayout.validateInput(messageInvalid: String? = null, checkIfValid: (String?) -> Boolean): Boolean {
val inputText = editText?.text?.toString()?.trim()
when {
inputText.isNullOrEmpty() -> {
error = context.getString(R.string.cant_be_empty)
}
!checkIfValid(inputText) -> {
error = messageInvalid
}
else -> {
error = null
return true
}
}
return false
}
那么你可以这样使用它:
val isCarbsValid = enterTextCarbs.validateInput(getString(R.string.invalid_value)) { it?.toDoubleOrNull() != null }
片段中有 5 个 TextInputEditText 字段。第一个是字符串,另外 4 个是用户必须输入的双精度值。 为确保值有效,检查每个字段是否为空,并检查最后四个(带双精度值)是否为双精度值。
在下面的代码片段中,我删除了最后 2 个 val 和 fun 声明,因为它们与最后 2 个 oes 完全相同,除了 TextInPutLayout 名称(和相应的 val)。
所以,我想知道是否有可能以任何方式缩短它
private val enterTextFoodName = view.findViewById<TextInputLayout>(R.id.enter_food_name)
private val enterTextKcal = view.findViewById<TextInputLayout>(R.id.enter_kcal)
private val enterTextCarbs = view.findViewById<TextInputLayout>(R.id.enter_carbs)
[...]
private fun validateValues(): Boolean {
return (!validateFoodName()
|| !validateKcal()
|| !validateCarbs()
[...])
}
private fun validateFoodName(): Boolean {
return when {
enterTextFoodName.editText?.text.toString().trim().isEmpty() -> {
enterTextFoodName.error = getString(R.string.cant_be_empty)
false
}
else -> {
enterTextFoodName.error = null
true
}
}
}
private fun validateKcal(): Boolean {
return when {
enterTextKcal.editText?.text.toString().trim().isEmpty() -> {
enterTextKcal.error = getString(R.string.cant_be_empty)
false
}
enterTextKcal.editText?.text.toString().trim().toDoubleOrNull() == null -> {
enterTextKcal.error = getString(R.string.invalid_value)
false
}
else -> {
enterTextKcal.error = null
true
}
}
}
private fun validateCarbs(): Boolean {
return when {
enterTextCarbs.editText?.text.toString().trim().isEmpty() -> {
enterTextCarbs.error = getString(R.string.cant_be_empty)
false
}
enterTextCarbs.editText?.text.toString().trim().toDoubleOrNull() == null -> {
enterTextCarbs.error = getString(R.string.invalid_value)
false
}
else -> {
enterTextCarbs.error = null
true
}
}
}
[...]
可以通过Kotlin中的扩展函数实现:
inline fun TextInputLayout.validateInput(messageInvalid: String? = null, checkIfValid: (String?) -> Boolean): Boolean {
val inputText = editText?.text?.toString()?.trim()
when {
inputText.isNullOrEmpty() -> {
error = context.getString(R.string.cant_be_empty)
}
!checkIfValid(inputText) -> {
error = messageInvalid
}
else -> {
error = null
return true
}
}
return false
}
那么你可以这样使用它:
val isCarbsValid = enterTextCarbs.validateInput(getString(R.string.invalid_value)) { it?.toDoubleOrNull() != null }