How to check if the field is filled in

Asked

Viewed 791 times

-1

I have some Edittext that must be filled. The button that checks if it is filled is an imagebutton in actionbar. I tried to do it in a way but it didn’t work.

the mistake is cnpj.setError(resources.getString(R.string.login_cnpj_required));

xml:

<string name="login_cnpj_required">Campo CNPJ obrigatório!</string>

code:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (cnpj.getText().toString().trim().equals("")) {
            cnpj.setError(resources.getString(R.string.login_cnpj_required));
        } else if (cnpj.getText().length() < 14) {
            cnpj.setError(resources.getString(R.string.login_cnpj_invalid));
        }


        if (id == R.id.action_pedido) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
  • What do you mean "it didn’t work"? And where is the statement of resources?

  • i got it, I did it through Boolean Boolean ok = true;

1 answer

1

In the second if you forgot the toString();

do:

String cnpj1 = cnpj.getText().toString();
If(!TextUtils.isEmpty(cnpj1) && cnpj1.length == 14){
   if(!isValidCnpj(cnpj1)) cnpj.setError("Mensagem"); 
}

Browser other questions tagged

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