【问题标题】:Is there a way to avoid repetition with similiar TextInputEditText validation?有没有办法避免重复使用类似的 TextInputEditText 验证?
【发布时间】:2020-02-10 02:16:38
【问题描述】:

片段中有 5 个 TextInputEditText 字段。第一个是字符串,另外 4 个是用户必须输入的 Doubles。 为确保值有效,检查每个字段是否为空,如果值为双精度则检查最后四个(带有双精度)。

在下面的代码中,我剪掉了最后 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
            }
        }
    }

[...]

【问题讨论】:

    标签: android kotlin android-textinputlayout android-textinputedittext


    【解决方案1】:

    你可以通过 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 }
    

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 1970-01-01
      • 2020-12-02
      • 2022-11-16
      • 2019-09-07
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多