【问题标题】:Android Don't dismiss AlertDialog after clicking PositiveButtonAndroid点击PositiveButton后不要关闭AlertDialog
【发布时间】:2014-11-23 01:48:53
【问题描述】:

我可以在单击 PositiveButton 后不关闭我的 AlertDialog 吗?

我想保留对话框以在我的 ArrayAdapter listWords 上显示一些更新。

这是我的代码。

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();

【问题讨论】:

标签: android android-alertdialog


【解决方案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();

【讨论】:

  • 谢谢 但是正面按钮好像没有功能。我在里面放了吐司,但是没有用。
  • 哈夫,终于做到了,现在它工作了,检查一下。使用 create 分配 AlertDialog,然后首先 setOnShowListener,然后调用 show()。它现在工作,希望它也适用于你。如果是这样,我要求您将其标记为答案,以便将来其他人可以轻松找到它。谢谢。
【解决方案2】:

基于How to prevent a dialog from closing when a button is clicked 投票最多的答案

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

                }
            });
        }
    });  

我相信您需要覆盖肯定按钮的处理程序。添加您的逻辑以在满足特定条件时关闭对话框。

【讨论】:

  • 将“AlertDialog.Builder d”更改为“AlertDialog d”后,我无法 setAdapter(listWords, null)。你能告诉我为什么吗?
  • 谢谢。为我工作:)
【解决方案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
            }
        });

【讨论】:

  • 这是最好的答案
【解决方案4】:

在 Kotlin 中回答:

val dialog = AlertDialog.Builder(context)
    .setView(v)
    .setTitle(R.string.my_title)
    .setPositiveButton(android.R.string.ok, null)
    .setNegativeButton(android.R.string.cancel, null)
    .create()

dialog.setOnShowListener {

        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
            // Apply logic here
        }

    }

【讨论】:

  • .setOnShowListener 和 .getButton 都不能用作 AlertDialog.Builder (Kotlin 1.3.10) 的公共方法。我错过了什么吗?
  • 这些方法包含在 AlertDialog 中。 AlertDialog 对话框 = new AlertDialog.Builder(context)........create();
【解决方案5】:

我是这样做的:

    final AlertDialog dialog = new AlertDialog.Builder(this)
            .setCancelable(false)
            .setPositiveButton("YES", null)
            .setNegativeButton("NO", null)
            .show();

    Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    positiveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
       //     Toast.makeText(SysManagerActivity.this, "dialog is open", Toast.LENGTH_SHORT).show();

        }
    });

【讨论】:

    猜你喜欢
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2018-11-11
    • 2019-06-21
    相关资源
    最近更新 更多