Close a Dialog When you click the back button

Asked

Viewed 187 times

1

I would like the open Dialog to be closed when I click on the return button of the mobile phone. I know that to close a dialog we use the Dismiss, but I don’t know how we declare the back button of the cell 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();

    }
}
  • 1

    Post the code of your dialog.

  • already friend, I edited.

1 answer

0


By default the dialog should end with the back button of Android, what may be happening is that you are using the method builder.setCancelable(false); and it prevents the dialog from being closed by any command other than the default buttons setNegativeButton or setPositiveButton. See your 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() {}

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

Browser other questions tagged

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