在Android上进行EditText验证的有效方法

Form validations on Android always takes extra effort than what is needed to achieve such a simple task. For example, multiple if-else statements or an external library that helps you to reduce the boilerplate. We are going to achieve this simply by using the kotlin extension function. This will help us reduce the boilerplate code and also make our code much more readable.

在Android上进行表单验证总是比完成这种简单任务所需的工作多。 例如,多个if-else语句或一个外部库可帮助您减少样板。 我们将仅通过使用kotlin扩展功能来实现此目的。 这将帮助我们减少样板代码,并使代码更具可读性。

Note: You can use the same extension method in Java too.

注意:您也可以在Java中使用相同的扩展方法。

fun TextInputEditText.isNotNullOrEmpty(errorString: String): Boolean {
    val textInputLayout = this.parent.parent as TextInputLayout
    textInputLayout.errorIconDrawable = null
    this.onChange { textInputLayout.error = null }


    return if (this.text.toString().trim().isEmpty()) {
        textInputLayout.error = errorString
        false
    } else {
        true
    }
}


fun EditText.isNotNullOrEmpty(errorString: String): Boolean {
    this.onChange { this.error = null }


    return if (this.text.toString().trim().isBlank()) {
        this.error = errorString
        false
    } else {
        true
    }
}


fun EditText.onChange(cb: (String) -> Unit) {
    this.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {
            cb(s.toString())
        }


        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
    })
}

Here, we are getting the TextInputLayout from TextInputEditText using this.parent.parent because TextInputLayout wraps the TextInputEditText into a LinearLayout. We are using this.onChange method so that whenever the user starts typing in the EditText the error disappears.

在这里,我们使用this.parent.parent从TextInputEditText获取TextInputLayout,因为TextInputLayout将TextInputEditText包装到LinearLayout中。 我们使用this.onChange方法,以便每当用户开始在EditText中键入内容时,错误就会消失。

if(TextInputEditText.isNotNullOrEmpty("This field is required") and
(TextInputEditText.isNotNullOrEmpty("This field is required")) { 
    //this will be executed only if the TextInputEditText contains
    // some text else error will be displayed by the extension function itself
}

To use the extension function in your activity you can trigger the above gist on click of the submit button. It will show the error, only if the validation fails else it will return true. Here, we have used logical “and” operator so that it will perform validation on the conditions mentioned inside the if statement.

要在您的活动中使用扩展功能,您可以在单击提交按钮时触发上述要点。 仅当验证失败时,它将显示错误,否则它将返回true。 在这里,我们使用了逻辑“ 和”运算符,以便它将对if语句中提到的条件执行验证。

Note: Use the logical “and” operator only if it is required.

注意:仅在需要时才使用逻辑“和”运算符。

This is how we can do validations effectively and simply. Do let me know your reviews in the comment section.

这就是我们可以有效而简单地进行验证的方式。 在评论部分让我知道您的评论。

Thanks for reading. Happy coding.

谢谢阅读。 快乐的编码。

翻译自: https://levelup.gitconnected.com/super-easy-way-to-do-edittext-validations-in-android-abb0f7446b14

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值