首页 > 解决方案 > 在完成所有验证之前如何防止导航?

问题描述

我想在我的表单中添加验证。但是有一个问题我按下提交按钮它会快速显示验证,并将空白表单提交给recyclerview。如果你有,请给我解决方案。

标签: androidformsvalidation

解决方案


    /**
     * Performs action to submit the form if all the validations are fulfilled
     */

    public void submitForm() {

        if (validateFields()) {
            //Todo add your form submission code here
        }
    }


    /**
     * Validate all the fields present in the form according to the requirements
     * Returns true if there is no validation error, false otherwise.
     */
    public boolean validateFields() {

        if (editTextEmail.getText().toString().isEmpty()) {
            //Show toast or snackbar for validation failed
            return false;
        } else if (//todo another validation code)
        {
        //Show toast or snackbar for validation failed
        return false;
    }
         return true;
}

推荐阅读