我的验证全部合而为一

时间:2018-08-05 04:18:49

标签: java android validation passwords

我在验证时遇到了一些问题。如何更改我的编码,以便一次进行一次验证。我正在观看许多教程,这里是结果。我希望我的验证一个接一个地进行。那么应该如何?预先感谢。

  private void RegisterAccount(String firstname, String lastname, String email, String password, String confirmpass)
{
    if (TextUtils.isEmpty(firstname))
    {
        Toast.makeText(Signup.this, "Enter your first name.", Toast.LENGTH_LONG).show();
    }

    if (TextUtils.isEmpty(lastname))
    {
        Toast.makeText(Signup.this, "Enter your last name.", Toast.LENGTH_LONG).show();
    }

    if (TextUtils.isEmpty(email))
    {
        Toast.makeText(Signup.this, "Enter your valid email address.", Toast.LENGTH_LONG).show();
    }

    if (TextUtils.isEmpty(password))
    {
        Toast.makeText(Signup.this, "Enter your password.", Toast.LENGTH_LONG).show();
    }

    if (TextUtils.isEmpty(confirmpass)) {
        Toast.makeText(Signup.this, "Please write your password again.", Toast.LENGTH_LONG).show();
    }

    if (password != confirmpass)
    {
        Toast.makeText(Signup.this, "Password do not match", Toast.LENGTH_LONG).show();
    }

    else
    {
        loadingBar.setTitle("Creating New Account");
        loadingBar.setMessage("Please wait while we are creating account for you.");
        loadingBar.show();

        mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>()
        {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task)
            {
                if(task.isSuccessful())
                {
                    Toast.makeText(Signup.this, "You have successfully signed up", Toast.LENGTH_SHORT).show();
                    Intent mainIntent = new Intent(Signup.this, MainActivity.class);
                    mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(mainIntent);
                    finish();
                }
                else
                {
                    Toast.makeText(Signup.this, "Error occured, please try again.", Toast.LENGTH_SHORT).show();
                }
                loadingBar.dismiss();
            }
        });
    }
}
}

2 个答案:

答案 0 :(得分:1)

您应该使用else -if代替if。

role="search"

答案 1 :(得分:0)

private boolean validateName() {
    if (inputName_reg.getText().toString().trim().isEmpty() ) {
        inputLayoutName_reg.setError(getString(R.string.err_msg_name));
        requestFocus(inputName_reg);
        return false;
    } else{
        inputLayoutName_reg.setErrorEnabled(false);
    }

    return true;
}

您需要使用“ else If”,而不仅仅是“ if”,这样代码才能更快地运行。使用布尔值进行验证,它们更具动态性。希望对您有帮助!!