java - 如何在 AlertDialog 中设置 AlertDialog.finish()?

标签 java android android-alertdialog

我目前正在使用需要使用大量 AlertDialogs 的应用程序。我目前在这里编写了一个基本的代码:

protected void StopButton () {
    AlertDialog.Builder StopDialog = new AlertDialog.Builder(MainActivity.this);
    StopDialog.setTitle(R.string.Stop_Title);
    StopDialog.setMessage(R.string.Stop_Message);
    StopDialog.setPositiveButton(R.string.Yes_Button, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            ((Protoype2) getApplication()).setRequestingLocationUpdates(false);
            finish();
        }
    });
    StopDialog.setNegativeButton(R.string.No_Button, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ((Protoype2) getApplication()).setRequestingLocationUpdates(true);
        }
    });
    StopDialog.setNeutralButton(R.string.Negative_Button, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //Closes box
            finish();
        }
    });
    AlertDialog alert = StopDialog.create();
    alert.show();
}

StopButton 起作用,当我调用它时 Dialog 出现。然而,finish();功能不起作用。

经过审查,我发现 finish();没有完成对话框,而是整个应用程序。我知道我需要在其中获取 AlertDialog.cancel。

问题是这样的:如您所见,AlertDialog 仅在 StopDialog 完成后创建。

如何在 StopDialog 完成之前设置 AlertDialog.finish()?

最佳答案

finish() 替换为 dialog.dismiss(),如下所示:

AlertDialog.Builder StopDialog = new AlertDialog.Builder(TestActivity.this);
        StopDialog.setTitle("Title");
        StopDialog.setMessage("Stop");
        StopDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                ((Protoype2) getApplication()).setRequestingLocationUpdates(false);
                dialog.dismiss();
            }
        });
        StopDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ((Protoype2) getApplication()).setRequestingLocationUpdates(true);
            }
        });
        StopDialog.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //Closes box
                dialog.dismiss();
            }
        });
        AlertDialog alert = StopDialog.create();
        alert.show();

关于java - 如何在 AlertDialog 中设置 AlertDialog.finish()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37084621/

相关文章:

android - 如何在对话框中显示编辑文本、 ListView 和正负按钮?

java - 无法通过 selenium 中的 htmlunit 驱动程序运行脚本

java - 在远程数据库中打开文件

java - AsyncTask 的 get() 方法 : Is there any scenario where it is actually the best option?

android - 在 android 客户端中获取 socket.io 响应,但无法理解如何在回收器适配器中实现响应

java - 如何为媒体音量变化注册 ContentObserver?

android - 删除 AlertDialog 背景

android - 如何定义新主题并覆盖框架中的警报对话框样式

java - 为什么在Java中从子类访问 protected 方法时会出现编译错误?

java - 如何使用反射来检查使用不同类加载器加载的类的非静态字段?