我正在为Android应用程序的注册表单工作,我正在尝试使用setError功能在用户单击注册按钮时验证文本字段。它正在运行检查,但仍在将表单提交给我的服务器进行注册。在提交注册用户的请求之前,我希望所有字段都有效。我一直在寻找解决方案几个小时,但我不知道我做错了什么。请帮忙!
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String first_name = inputFirstName.getText().toString();
String last_name = inputLastName.getText().toString();
String email = inputEmail.getText().toString();
String dob = displaytxt.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(first_name, last_name, email, dob, password);
// check for login response
try {
if(inputFirstName.getText().toString().length() <= 0)
{
inputFirstName.setError("First name required!");
}
if (inputLastName.getText().toString().length() < 0)
{
inputLastName.setError("Last name required!");
}
if (inputEmail.getText().toString().length() == 0)
{
inputEmail.setError( "Valid email address required!" );
}
if (inputPassword.getText().toString().length() < 8)
{
inputPassword.setError( "Password required! (Minimum of 8 characters." );
}
if (!inputPassword2.getText().toString().equals(inputPassword.getText().toString()))
{
inputPassword.setError( "Passwords do not match! (Minimum of 8 characters.");
inputPassword2.setError("Passwords do not match! (Minimum of 8 characters.");
}
if (!checkBoxTerms.isChecked());
{
AlertDialog alertDialog = new AlertDialog.Builder(RegisterActivity.this).create();
alertDialog.setTitle("oops!");
alertDialog.setMessage("You must agree to the terms & conditions.");
alertDialog.setButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
alertDialog.show();
}
if (json.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully registred
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_FIRST_NAME), json_user.getString(KEY_LAST_NAME), json_user.getString(KEY_EMAIL), json_user.getString(KEY_DOB), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), MainActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Registration Screen
finish();
}else{
// Error in registration
registerErrorMsg.setText("Error in registration! Please check the information entered.");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
答案 0 :(得分:2)
汇总所有错误,并且仅在从onClick
返回后使用错误标志,例如:
boolean isError = false;
if(inputFirstName.getText().toString().length() <= 0)
{
inputFirstName.setError("First name required!");
isError = true;
}
if (inputLastName.getText().toString().length() < 0)
{
inputLastName.setError("Last name required!");
isError = true;
}
if (inputEmail.getText().toString().length() == 0)
{
inputEmail.setError( "Valid email address required!" );
isError = true;
}
if (inputPassword.getText().toString().length() < 8)
{
inputPassword.setError( "Password required! (Minimum of 8 characters." );
isError = true;
}
if (!inputPassword2.getText().toString().equals(inputPassword.getText().toString()))
{
inputPassword.setError( "Passwords do not match! (Minimum of 8 characters.");
inputPassword2.setError("Passwords do not match! (Minimum of 8 characters.");
isError = true;
}
if(isError){
return;
}
答案 1 :(得分:0)
试试这个对我来说很完美
buttonClicked.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (lastName.getText().toString().trim().equalsIgnoreCase("")) {
lastName.setError("Enter LastName");
}else{
//call your intent
}
}
});
lastName.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
lastName.setError(null);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
lastName.setError(null);
}
});
答案 2 :(得分:0)
您可以在此处尝试导入此课程 Android Form Validation Class
这可以帮助您验证表单字段,只需按照说明操作即可。 (: