-1
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 类?

4

3 回答 3

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));
于 2017-12-28T05:20:38.967 回答
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));
}
于 2017-12-28T05:23:25.217 回答
0

创建方法布尔检查

       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;
    }
于 2017-12-28T05:35:40.283 回答