How to check if the field is filled [closed]

-1

I have some EditText that should be filled out. The button that checks if it is filled is an imagebutton in the actionbar. I tried to do it one way but it did not work.

error 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);
    }
    
asked by anonymous 25.07.2016 / 23:44

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"); 
}
    
26.07.2016 / 14:03