Подтвердить что ты не робот

Android Не отключайте AlertDialog после нажатия кнопки PositiveButton

Можно ли просто не отклонить мой AlertDialog после нажатия кнопки PositiveButton?

Я хотел бы остаться в диалоговом окне, чтобы показать что-то обновление в моем списке слов ArrayAdapter.

Это мой код. Спасибо за помощь:)

    AlertDialog.Builder sayWindows = new AlertDialog.Builder(MapActivity.this);

    final EditText saySomething = new EditText(MapActivity.this);

    sayWindows.setPositiveButton("ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    say = userName + " Says: "+saySomething.getText();
                    showPosition.setText(say);                      
                }
            });

    sayWindows.setNegativeButton("cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

    sayWindows.setAdapter(listWords, null);
    sayWindows.setView(saySomething);
    sayWindows.create().show();
4b9b3361

Ответ 1

Посмотрев на решение @Little Child, я пытаюсь это сделать. Сообщите нам, если это сработает для вас.

    AlertDialog.Builder sayWindows = new AlertDialog.Builder(
            MapActivity.this);
    final EditText saySomething = new EditText(MapActivity.this);
    sayWindows.setPositiveButton("ok", null);
    sayWindows.setNegativeButton("cancel", null);
    sayWindows.setAdapter(listWords, null);
    sayWindows.setView(saySomething);

    final AlertDialog mAlertDialog = sayWindows.create();
    mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {

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

                @Override
                public void onClick(View view) {
                    // TODO Do something
                   say = userName + " Says: "+saySomething.getText();
                   showPosition.setText(say); 
                }
            });
        }
    });
    mAlertDialog.show();

Ответ 2

на основе самого голосованного ответа для Как предотвратить закрытие диалогового окна при нажатии кнопки

final AlertDialog d = new AlertDialog.Builder(context)
            .setView(v)
            .setTitle(R.string.my_title)
            .setPositiveButton(android.R.string.ok, null) //Set to null. We override the onclick
            .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) {
                    // TODO Do something

                }
            });
        }
    });  

Я считаю, что вам нужно переопределить обработчик положительной кнопки. Добавьте свою логику, чтобы отклонить диалог, когда выполняется определенное условие.

Ответ 3

Даже простой:

final AlertDialog alertDialog = new AlertDialog.Builder(context).setView(v)
                .setPositiveButton(android.R.string.ok, null)
                .setNegativeButton(android.R.string.cancel, null)
                .show();

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

            @Override
            public void onClick(View view) {
                //Do Your thing
            }
        });