0

我正在为一个 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();
            }
        }
    });
4

3 回答 3

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;
   }  
于 2013-09-16T07:17:20.007 回答
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);
                }
            });
于 2013-09-16T07:20:27.693 回答
0

您可以尝试在此处导入此类 Android Form Validation Class

这可以帮助您验证表单字段,只需按照说明进行操作。(:

于 2013-12-04T09:20:35.223 回答