在 Android 中显示所有 setError 消息

Show all setError messages in Android

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));

如果两个文本输入布局留空,我如何才能显示两个错误消息,并确保用户仅在正确填写密码的情况下无法继续访问更多信息class?

使用基于验证您的条件的布尔变量,然后导航。试试下面的示例

 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));

创建一个函数,其中 returns 一个布尔值并根据该值进行相应操作:

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));
}

创建要检查的布尔值方法

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

布尔方法

    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;
    }