以下代码是应用程序注册页面的一部分。我最近问了一个关于我的代码中的错误的问题,我被告知Handler
总是在调用onSignupSuccess
。如何确保根据布尔emailsuccess
的成功或失败,Handler
将运行onSignupSuccess
和onSignupFailed
,或else
下的代码声明分别?
link_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent login = new Intent(SignUp.this, Login.class);
startActivity(login);
}
});
signupbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signup();
}
});
}
public void signup() {
Log.d(TAG, "Signup");
if(!validate()) {
onSignupFailed();
return;
}
signupbutton.setEnabled(false);
final ProgressDialog progressDialog = new ProgressDialog(SignUp.this, R.style.Theme_AppCompat_DayNight_Dialog_Alert);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Creating Account");
progressDialog.show();
final String name = inputname.getText().toString();
final String email = inputemail.getText().toString();
final String password = inputpassword.getText().toString();
final int number = Integer.parseInt(inputnumber.getText().toString());
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean emailsuccess = jsonResponse.getBoolean("emailsuccess");
if (emailsuccess) {
onSignupSuccess();
return;
}
else {
onSignupFailed();
AlertDialog.Builder cancel = new AlertDialog.Builder(SignUp.this);
cancel.setTitle("Registration Failed");
cancel.setMessage("Email already in database.");
cancel.setNegativeButton("Retry", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
cancel.create();
cancel.show();
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(name, email, password, number, responseListener);
RequestQueue queue = Volley.newRequestQueue(SignUp.this);
queue.add(registerRequest);
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
onSignupSuccess();
progressDialog.dismiss();
}
}, 3000);
}
public void onSignupSuccess() {
signupbutton.setEnabled(true);
setResult(RESULT_OK, null);
AlertDialog.Builder builder = new AlertDialog.Builder(SignUp.this);
builder.setTitle("Registration Successful!");
builder.setMessage("A confirmation code has been sent to your email.");
builder.setPositiveButton("Next", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(SignUp.this, Confirmation_Sign_Up.class);
startActivity(intent);
}
});
builder.create();
builder.show();;
}
public void onSignupFailed() {
Toast toast = Toast.makeText(this, "Registration failed", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
v.setTextColor(Color.RED);
toast.show();
signupbutton.setEnabled(true);
}
public boolean validate() {
boolean valid = true;
String name = inputname.getText().toString();
String email = inputemail.getText().toString();
String password = inputpassword.getText().toString();
String number = inputnumber.getText().toString();
if (name.isEmpty() || name.length() < 2) {
inputname.setError("Store name must be at least 2 characters");
valid = false;
} else {
inputname.setError(null);
}
if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
inputemail.setError("Enter a valid email address");
valid = false;
} else {
inputemail.setError(null);
}
if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
inputpassword.setError("Password must have between 4 and 10 characters");
valid = false;
} else {
inputpassword.setError(null);
}
if (number.isEmpty() || number.length() != 8) {
inputnumber.setError("Enter a valid phone number");
valid = false;
} else {
inputnumber.setError(null);
}
return valid;
}
}
编辑:我删除了处理程序并将ProgressDialog
放在try
下,但现在ProgressDialog
并没有解雇。
try {
final ProgressDialog progressDialog = new ProgressDialog(SignUp.this, R.style.Theme_AppCompat_DayNight_Dialog_Alert);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Creating Account");
progressDialog.show();
JSONObject jsonResponse = new JSONObject(response);
boolean emailsuccess = jsonResponse.getBoolean("emailsuccess");
if (emailsuccess) {
progressDialog.dismiss();
onSignupSuccess();
}
else ........