在Android中显示所有setError消息

时间:2017-12-28 05:16:19

标签: android android-textinputlayout textinputlayout

if (!validateEmail(email) || (TextUtils.isEmpty(email))) {
        emailwrapper.setError("Invalid email");
    } else {
        emailwrapper.setError(null);
    }

    if (password.length() < 6 || (TextUtils.isEmpty(password))) {
        passwordwrapper.setError("Password must have at least 6 characters");
        return;

    } else {
        passwordwrapper.setError(null);
    }
    startActivity(new Intent(SignupActivity.this, MoreInfo.class));

如果将两个文本输入布局留空并且确保用户无法在只填写正确密码的情况下继续使用MoreInfo类,如何显示两条错误消息?

3 个答案:

答案 0 :(得分:1)

使用基于验证条件的布尔变量然后导航。请尝试以下示例

 boolean valid = true;

    if (!validateEmail(email) || (TextUtils.isEmpty(email))) {
            valid = false;
            emailwrapper.setError("Invalid email");
        } else {
            valid = true;
            emailwrapper.setError(null);
        }

        if (password.length() < 6 || (TextUtils.isEmpty(password))) {
             valid = false;
            passwordwrapper.setError("Password must have at least 6 characters");

        } else {
             valid = true;
            passwordwrapper.setError(null);
        }

        if(valid)
          startActivity(new Intent(SignupActivity.this, MoreInfo.class));

答案 1 :(得分:1)

创建一个函数,该函数返回一个布尔值,并根据该值继续进行:

private boolean areAllFieldsValid() {
    boolean returnValue = true;
    if (!validateEmail(email) || (TextUtils.isEmpty(email))) {
        emailwrapper.setError("Invalid email");
        returnValue = false;
    } else {
        emailwrapper.setError(null);
    }

    if (password.length() < 6 || (TextUtils.isEmpty(password))) {
        passwordwrapper.setError("Password must have at least 6 characters");
        returnValue = false;
    } else {
        passwordwrapper.setError(null);
    }
    return returnValue;
}

现在使用以下方法检查此情况:

if(areAllFieldsValid()){
    startActivity(new Intent(SignupActivity.this, MoreInfo.class));
}

答案 2 :(得分:0)

创建方法布尔值以检查

       if(isValid()){
                startActivity(new Intent(SignupActivity.this, MoreInfo.class));
            }

Boolean Mehtod

    private boolean isValid() {
    if (!validateEmail(email) || (TextUtils.isEmpty(email))) {
    emailwrapper.setError("Invalid email");
    return false;
    } else {
    emailwrapper.setError(null);
    emailwapper.setErrorEnabled(false);
    }

    if (password.length() < 6 || (TextUtils.isEmpty(password))) {
    passwordwrapper.setError("Password must have at least 6 characters");
    return false;

    } else {
    passwordwrapper.setError(null);
    passwordwapper.setErrorEnabled(false);
    }

    retun true;
    }