What code do I use to create a Validate class on Androidstudio?

Asked

Viewed 62 times

1

I want to create a class to check the 2 fields EditText are filled, called Validacao. I wanted to call like:

if (Validacao(TextoA, TextoB) == true) {
    executa código;

I don’t know what the class code looks like Validacao. You have to have a builder Validacao()? What the code would look like?

import android.widget.EditText;
import android.widget.Toast;

public class Validacao {
    private boolean x = true;
    private boolean y = true;
    private boolean z = true;
    private boolean a = false;

    public Validacao(EditText textA, EditText textB) {
        // Valida para ter todos os 2 campos preenchidos
        if ((textA.getText().toString().isEmpty()) && (textB.getText().toString().isEmpty())) {
            z = false;
            Toast toast = Toast.makeText(this, "Informe os números." , Toast.LENGTH_SHORT);
            toast.show();
        } else if (textA.getText().toString().isEmpty()) {
            x = false;
            Toast toast = Toast.makeText(this, "Informe o primeiro número." , Toast.LENGTH_SHORT);
            toast.show();
        } else if (textB.getText().toString().isEmpty()) {
            y = false;
            Toast toast = Toast.makeText(this, "Informe o segundo número." , Toast.LENGTH_SHORT);
            toast.show();
        }
    }
    if (x && y && z)
        a = true;
    return a;
}
  • if(Nomedavariavel.gettext().toString().equals(" ")){ Toast.makeText(Mainactivity.this,"Please fill in the text", Toast.LENGTH_SHORT). show(); I don’t know if this was the doubt, but to make the validation if the text is not null is like this.

1 answer

0


When a function with the same class name is defined, that function shall be the constructor of that class, and shall be invoked through the operator new

As you are creating a validation class, I suggest you use static functions and the logic of checking how many valid variables are made where these methods are called

For example:

public class Validacao {
    public Validacao() {  }

    public static boolean isValid(EditText et, String msg, Context context) {
        if (et.getText().toString().isEmpty()) {
            Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
            toast.show();

            return false;
        } else {
            return true;
        }
    }
}

To use:

if (Validacao.isValid(x, "Informe o primeiro número", getApplicationContext()) && Validacao.isValid(y, , "Informe o segundo número", getApplicationContext()) && Validacao.isValid(z, "Informe o terceiro número", getApplicationContext())) {
    // Faz alguma coisa
}

You can also create a custom function for each variable

  • It’s not working. It would be public Static Boolean isValid(Edittext et, String msg) { ... }? Even so, it gives error in class Validation, that in Androidstudio.

  • Yes, I missed to define the type, for a while I do not touch with java, I forgot to put, what error appears? I believe the problem is this this, try to use getApplicationContext()

  • @Joãoricardo edited the question, check if it meets what you need

  • Thanks William, but I’ve already managed to run the example

Browser other questions tagged

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