【问题标题】:Get Dialog user input before remaining code executes在剩余代码执行之前获取对话框用户输入
【发布时间】:2014-02-23 15:33:55
【问题描述】:

我正在尝试从对话框提示中获取用户输入,以确定变量是真还是假。 我需要在执行 List Activity 类中的任何其他代码之前执行此操作。

问题是无论我在哪里调用显示提示的方法,类中的其余代码都会在对话框返回用户选择之前继续执行!

哪里是放置对话框提示和停止执行代码表单的最佳位置,直到我们通过对话框得到用户的响应?

谢谢 夏兰

PS 这是我在 ListActovty onCreate 方法中调用的代码:

private void previewDives() {
        //ask user if they wish to preview images in ist view as will take more time
                AlertDialog.Builder build = null;
                AlertDialog dialog = null;
                build = new AlertDialog.Builder(this);
                build.setCancelable(true);
                build.setMessage("Do you want to preview your Dive Site images? \nThis may take more time if you have a large number of dives");
                build.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // preview dives
                                preiwImagesInListView= true;
                                value++;
                                //return;//exit

                            }// end on click
                        });// end pos button take photo

                // get photo from SD card option
                build.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                preiwImagesInListView=false;
                                value++;
                                //return;//exit to oncreate

                            }// end onclick
                        });// end pos button take photo
                dialog = build.create();
                dialog.setTitle("Preview Dive Images");
                dialog.show();

                //return preiwImagesInListView;


    }

【问题讨论】:

    标签: android dialog


    【解决方案1】:

    对话框在不同的线程中执行。所以无论你在show() 调用之后做什么,都会被执行。要解决此问题,请创建一个函数来处理函数的响应,并在 YesNo 带有参数的答案函数中调用该函数。如果您使用多个线程,更好的解决方案是使用MessageHandler 使其线程安全。

    你不能让线程等待。更多信息可以在这个线程上找到Dialogs / AlertDialogs: How to "block execution" while dialog is up (.NET-style)

    【讨论】:

      【解决方案2】:

      您可以将您的代码放入Dialog Yes 按钮onClick() 事件中。这是因为在获得数据以确认您要处理更多或不处理的用户之后。但在将代码执行到是按钮onclick() 之前,您必须检查用户输入是否有效。

      【讨论】:

        猜你喜欢
        • 2015-10-25
        • 1970-01-01
        • 2016-05-27
        • 1970-01-01
        • 1970-01-01
        • 2012-08-02
        • 1970-01-01
        • 1970-01-01
        • 2011-11-28
        相关资源
        最近更新 更多