Error validating android Studio field

Asked

Viewed 864 times

0

Good morning Everybody, I’m making the following mistake:

When trying to validate my fields by clicking the sign up button, the same does no action.. I’m new with android and would like a help.

Thank you.


package com.example.eduardo.jfdigital;

import android.support.annotation.Nullable;
import android.support.annotation.StringDef;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.eduardo.jfdigital.R;

import java.util.regex.Pattern;

import static com.example.eduardo.jfdigital.R.id.confSenha;
import static com.example.eduardo.jfdigital.R.id.email;
import static com.example.eduardo.jfdigital.R.id.frame_layout;
import static com.example.eduardo.jfdigital.R.id.login;
import static com.example.eduardo.jfdigital.R.id.nome;
import static com.example.eduardo.jfdigital.R.id.senha;

public class efetuarcadastro extends android.support.v4.app.Fragment {

    View myView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        myView = inflater.inflate( R.layout.efetuarcadastro, container,false);

        myView.findViewById( R.id.cad );
        myView.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                validaCampos();
            }


     /*   nome.setOnFocusChangeListener( new View.OnFocusChangeListener() {
            @Override
            public String toString() {
                return "$classname{}";
            }

            @Override
            public void onFocusChange(View view, boolean b) {


                if ((nome.getText().length() < 1))

                {
                    nome.setError( "Campo obrigatório" );
                }

                else
                    if ((login.getText().length() < 1))

                {
                    login.setError( "Campo obrigatório" );
                }

                else
                    if ((email.getText().length() < 1))

                {
                    email.setError( "Campo obrigatório" );
                }


            }

        } ); */

            EditText nome  = myView.findViewById( R.id.nome );
            EditText login = myView.findViewById( R.id.login );
            EditText email = myView.findViewById( R.id.email );
            EditText senha = myView.findViewById( R.id.senha );
            EditText confSenha = myView.findViewById( R.id.confSenha );

            /* nome =      (EditText) findViewById( R.id.nome);
            login =     (EditText) findViewById(R.id.login);
            email =     (EditText) findViewById( R.id.email);
            senha =     (EditText) findViewById( R.id.senha);
            confSenha = (EditText) findViewById( confSenha); */


            private void validaCampos() {

                boolean res = false;
                String name = nome.getText().toString();
                String user = login.getText().toString();
                String emails = email.getText().toString();
                String password = senha.getText().toString();
                String confpass = confSenha.getText().toString();

                if (res = verificaCampovazio( name )) {
                    nome.requestFocus();
                } else
                if (res = verificaCampovazio( user )) {
                    login.requestFocus();
                    AlertDialog.Builder Mcampovazio = new AlertDialog.Builder(efetuarcadastro.this.getActivity());
                } else
                if (res = !emailValidar( emails )) {
                    email.requestFocus();
                } else
                if (res = verificaCampovazio( password )) {
                    senha.requestFocus();
                } else
                if (res = verificaCampovazio( confpass )) {
                    confSenha.requestFocus();

                    if (res) {
                        AlertDialog.Builder Mcampovazio = new AlertDialog.Builder(efetuarcadastro.this.getActivity());
                        Mcampovazio.setTitle( "Aviso: " );
                        Mcampovazio.setMessage( "Há campos inválidos ou sem preenchimento" );
                        Mcampovazio.setNeutralButton( "OK",null );
                        Mcampovazio.show(); }

                }
            }

            private boolean verificaCampovazio(String valor) {

                Boolean resultado = (TextUtils.isEmpty( valor ) || valor.trim().isEmpty());

                return resultado;
            }

            private boolean emailValidar(String email) {

                Boolean resultado = (!verificaCampovazio( email ) && Patterns.EMAIL_ADDRESS.matcher( email ).matches());

                return resultado;


            }

        } );

        return myView;
    }

}
  • What action you would like to implement or want done?

  • I would like if there is a blank field, generate an alert "There are invalid fields or no padding" when clicking the sign up button and the email field implement the validation of the field not to enter an invalid email.

1 answer

0


The Warning of the Method validaCampos() is within the last field condition, in case the field confpass. That is, it will only display if the condition confpass is true. Adjust as follows:

private void validaCampos() {

    boolean res = false;
    String name = nome.getText().toString();
    String user = login.getText().toString();
    String emails = email.getText().toString();
    String password = senha.getText().toString();
    String confpass = confSenha.getText().toString();

    if (res = verificaCampovazio(name)) {
        nome.requestFocus();
    } else if (res = verificaCampovazio(user)) {
        login.requestFocus();
        AlertDialog.Builder Mcampovazio = new AlertDialog.Builder(efetuarcadastro.this.getActivity());
    } else if (res = !emailValidar(emails)) {
        email.requestFocus();
    } else if (res = verificaCampovazio(password)) {
        senha.requestFocus();
    } else if (res = verificaCampovazio(confpass)) {
        confSenha.requestFocus();
    }
    if (res) {
        AlertDialog.Builder Mcampovazio = new AlertDialog.Builder(efetuarcadastro.this.getActivity());
        Mcampovazio.setTitle("Aviso: ");
        Mcampovazio.setMessage("Há campos inválidos ou sem preenchimento");
        Mcampovazio.setNeutralButton("OK", null);
        Mcampovazio.show();
    }

}
  • Thank you very much! it worked out thanks!

  • Not at all! Check as solved to make it easier to view pending questions. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.