How do I make the Button work only if EditText is populated?

0

I want you to just push the button after all EditText 's are filled

package com.example.marcosnogueira.appnote;


import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.content.*;

public class Principal extends AppCompatActivity {

    private EditText nome;
    private EditText nomlojcli;
    private EditText endereco;
    private EditText cpf;
    private EditText cnpj;
    private EditText tel;
    private Button cadastrar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);

        nome = (EditText) findViewById(R.id.nome);
        nomlojcli = (EditText) findViewById(R.id.nomlojcli);
        endereco = (EditText) findViewById(R.id.endereco);
        cpf = (EditText) findViewById(R.id.cpf)
        cnpj = (EditText) findViewById(R.id.cnpj);
        tel = (EditText) findViewById(R.id.tel);
        cadastrar = (Button) findViewById(R.id.button);

        cadastrar.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder mensagem = new AlertDialog.Builder(Principal.this);
                mensagem.setMessage("Cadastrado com Sucesso");
                mensagem.setNeutralButton("Ok",null);
                mensagem.show();


            }
        });
    }
}
    
asked by anonymous 22.10.2017 / 06:18

1 answer

3

Thinking here, I was able to find two ways to do this.

  • The first one (which I think is best for explaining the "error", but not exactly what you asked for) is to leave the button clickable and when clicking make condição checking if all EditText.length if not, show AlertDialog , otherwise, show Toast % speaking missing information.

    eTNome = (EditText)findViewById(R.id.eTNome);
    eTCidade = (EditText)findViewById(R.id.eTCidade);
    btnSave = (Button)findViewById(R.id.btnSave);
    
    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(eTCidade.length() != 0 && eTNome.length() != 0){
                //Mensagem avisando "cadastro feito com sucesso"!
            } else {
                //Avisando pra colocar mais informações
            }
        }
    });
    
  • The other solution (which is what you want) would be with every change in a EditText check that all are filled and if they are leaving the button clickable, for that we can use addTextChangedListener , with it you can do actions before, during and after the text is changed, overwriting the methods beforeTextChanged , onTextChanged and afterTextChanged , I only used the after, but I think in any one it serves, the code below:

    public class MainActivity extends AppCompatActivity {
        Button btnSave;
        EditText eTNome;
        EditText eTCidade;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            eTNome = (EditText)findViewById(R.id.eTNome);
            eTCidade = (EditText)findViewById(R.id.eTCidade);
            btnSave = (Button)findViewById(R.id.btnSave);
            btnSave.setEnabled(false);
            btnSave.setClickable(false);
    
            eTNome.addTextChangedListener(MudarTexto);
            eTCidade.addTextChangedListener(MudarTexto);
        }
        private TextWatcher MudarTexto = new TextWatcher(){
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                AoMudarTexto();
            }
        };
        private void AoMudarTexto(){
            if(eTCidade.length() != 0 && eTNome.length() != 0) {
                btnSave.setEnabled(true);
                btnSave.setClickable(true);
            } else {
                btnSave.setEnabled(false);
                btnSave.setClickable(false);
            }
        }
    }
    
  • 22.10.2017 / 18:23