Can't you not turn it off when you click the button in the dialog?

Asked 1 years ago, Updated 1 years ago, 76 views

I made a dialog with EditText. There are "Yes" and "No" buttons. For example, if you click the "Yes" button, the input is checked and the dialog is turned off. But even if the input is wrong, the dialog is turned off. Even if the button is pressed, if the input value is wrong, it cannot be turned off?

android dialog android-alertdialog android-dialog android-dialogfragment

2022-09-22 22:34

1 Answers

This should only work with API 8 and above.

In AlertDialog's onShowListener, you can override the onClickListener buttonYo

final AlertDialog d = new AlertDialog.Builder(context)
        .setView(v)
        .setTitle(R.string.my_title)
        .setPositiveButton(android.R.string.ok, null) //onClick. Please make it null.
        .setNegativeButton(android.R.string.cancel, null)
        .create();

d.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                            // giving a condition

                            // If the input value is good, dialog is off
                d.dismiss();
            }
        });
    }
});


2022-09-22 22:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.