Close a Dialogo When you click the back button

1

I would like the Dialogo open to be closed when I click the return button on the phone. I know that to close a dialog we use dismiss , but I do not know how we declare the back button on the phone.

public class DialogValor {
    Button butNum1, butNum2, butNum3, butNum4, butNum5, butNum6, butNum7, butNum8, butNum9, butVirgula, butNum0, butOk;
    TextView txtValorADD;
    ImageButton imbClear;
    public interface FinalizarDialogo
    {
        void ResultadoDialogo (String txtValorADD);
    }
    private FinalizarDialogo interfaz;

    public DialogValor(final Context context, FinalizarDialogo actividad) {
        interfaz = actividad;
        final Dialog dialogo = new Dialog(context);
        dialogo.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogo.setCancelable(false);
        dialogo.setContentView(R.layout.alerta);

        txtValorADD = (TextView) dialogo.findViewById(R.id.txtValorADD);
        imbClear = (ImageButton) dialogo.findViewById(R.id.imbClear);
        butNum0 = (Button) dialogo.findViewById(R.id.butNum0);
        butNum1 = (Button) dialogo.findViewById(R.id.butNum1);
        butNum2 = (Button) dialogo.findViewById(R.id.butNum2);
        butNum3 = (Button) dialogo.findViewById(R.id.butNum3);
        butNum4 = (Button) dialogo.findViewById(R.id.butNum4);
        butNum5 = (Button) dialogo.findViewById(R.id.butNum5);
        butNum6 = (Button) dialogo.findViewById(R.id.butNum6);
        butNum7 = (Button) dialogo.findViewById(R.id.butNum7);
        butNum8 = (Button) dialogo.findViewById(R.id.butNum8);
        butNum9 = (Button) dialogo.findViewById(R.id.butNum9);
        butVirgula = (Button) dialogo.findViewById(R.id.butVirgula);
        butOk = (Button) dialogo.findViewById(R.id.butOk);
        butNum0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtValorADD.append("0");
            }
        });
        butNum1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtValorADD.append("1");
            }
        });
        butNum2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtValorADD.append("2");
            }
        });
        butNum3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtValorADD.append("3");
            }
        });
        butNum4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtValorADD.append("4");
            }
        });
        butNum5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtValorADD.append("5");
            }
        });
        butNum6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtValorADD.append("6");
            }
        });
        butNum7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtValorADD.append("7");
            }
        });
        butNum8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtValorADD.append("8");
            }
        });
        butNum9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtValorADD.append("9");
            }
        });
        butVirgula.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                txtValorADD.append(",");
            }
        });
        imbClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(txtValorADD.getText().length()==0)
                {

                }
                else {
                    txtValorADD.setText(txtValorADD.getText().subSequence(0, txtValorADD.getText().length() - 1));
                }

            }
        });
        butOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (txtValorADD.getText().length()==0)
                {
                    dialogo.dismiss();
                }
                else{
                    interfaz.ResultadoDialogo(txtValorADD.getText().toString());
                dialogo.dismiss();}

            }
        });

      dialogo.show();

    }
}
    
asked by anonymous 27.05.2018 / 19:35

1 answer

0

By default the dialog should terminate with the Android back button, what is happening is that you are using the builder.setCancelable(false); method and it prevents the dialog from being closed by any command other than the default buttons setNegativeButton or setPositiveButton . Here's the example below that should close:

 public DialogValor(final Context context, FinalizarDialogo actividad) {
    interfaz = actividad;
    final Dialog dialogo = new Dialog(context);
    dialogo.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialogo.setContentView(R.layout.alerta);

    txtValorADD = (TextView) dialogo.findViewById(R.id.txtValorADD);
    imbClear = (ImageButton) dialogo.findViewById(R.id.imbClear);
    butNum0 = (Button) dialogo.findViewById(R.id.butNum0);
    butNum1 = (Button) dialogo.findViewById(R.id.butNum1);
    butNum2 = (Button) dialogo.findViewById(R.id.butNum2);
    butNum3 = (Button) dialogo.findViewById(R.id.butNum3);
    butNum4 = (Button) dialogo.findViewById(R.id.butNum4);
    butNum5 = (Button) dialogo.findViewById(R.id.butNum5);
    butNum6 = (Button) dialogo.findViewById(R.id.butNum6);
    butNum7 = (Button) dialogo.findViewById(R.id.butNum7);
    butNum8 = (Button) dialogo.findViewById(R.id.butNum8);
    butNum9 = (Button) dialogo.findViewById(R.id.butNum9);
    butVirgula = (Button) dialogo.findViewById(R.id.butVirgula);
    butOk = (Button) dialogo.findViewById(R.id.butOk);
    butNum0.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtValorADD.append("0");
        }
    });
    butNum1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtValorADD.append("1");
        }
    });
    butNum2.setOnClickListener(new View.OnClickListener() {}

Now, remove the line builder.setCancelable(false); that everything should work as you expect. Or change to builder.setCancelable(true);

    
27.05.2018 / 20:41