G
G
goodTimes2017-03-15 00:14:39
Java
goodTimes, 2017-03-15 00:14:39

How to handle click of alertDialog button?

AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                        builder.setView(promptsView);
                        builder.setTitle("Введите имя пользователя")
                               // .setMessage("Введите имя")
                                .setCancelable(false)
                                .setPositiveButton("ОК",
                                        new DialogInterface.OnClickListener() {
                                            public void onClick(DialogInterface dialog, int id) {
                                                if (userInput.getText().toString().equals(""))
                                                {
                                                    Toast toast = Toast.makeText(view.getContext(),
                                                            "Заполните поля!", Toast.LENGTH_SHORT);
                                                    toast.show();
                                                }else {
                                                    dialog.cancel();
                                                }
                                            }
                                        });
                        AlertDialog alert = builder.create();
                        alert.show();

If the field is empty, then the dialog box should not close, but it closes. Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2017-03-15
@LenLord

First define a dialog with an empty onClick
And then override onClick further:

adb.setView(view).setTitle(R.string.fw)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        //Ничего здесь не определеяем
                    }
                });

What next:
Button positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
               // Пишите тут свою логику
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question