【问题标题】:Is there something like javascript 'confirm(msg)' in Android?Android中有类似javascript'confirm(message)'的东西吗?
【发布时间】:2017-07-23 07:11:44
【问题描述】:

我需要 Android 中的 javascript 'confirm(msg)' 等功能。 类似 AlertDialog 的东西,但有可能返回值,而不是回调。 有什么方法可以实现吗?

更新 像这样的功能不适合我的问题:

builder.setPositiveButton("Yes", new OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        method1();
    });
builder.setNegativeButton("No", new OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        method2();
    });

我需要这样的东西:

if (confirmDialog.askConfirm()) {
    method1();
} else {
    method2();
}

【问题讨论】:

    标签: javascript android android-alertdialog confirm


    【解决方案1】:

    我已经通过这种方式解决了我的问题:

    public class JSStyleConfirmDialog {
        private volatile boolean interrupted = false;
        private volatile boolean result;
        private volatile boolean resultObtained = false;
    
        public static final int YES = 1;
        public static final int NO = 0;
        public static final int ERROR = -1;
    
        public int askConfirm(final ClientActivity activity, final String msg) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    try {
                        while (!resultObtained) {
                            Thread.sleep(20);
                        }
                    } catch (InterruptedException e) {
                        interrupted = true;
                    }
                }
            };
            thread.start();
    
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                    builder.setMessage(msg);
                    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            result = true;
                            resultObtained = true;
                        }
                    });
                    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            result = false;
                            resultObtained = true;
                        }
                    });
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
            });
    
    
            try {
                thread.join();
            } catch (InterruptedException e) {
                return ERROR;
            }
    
            if (interrupted) {
                return ERROR;
            }
    
            return result?YES:NO;
        }
    }
    

    这必须从非ui线程调用:

    int result = new JSStyleConfirmDialog().askConfirm(activity, msg);
    

    【讨论】:

      【解决方案2】:

      很确定这就是您要查找的内容: https://developer.android.com/guide/topics/ui/dialogs.html

      【讨论】:

      • 我很清楚,但这不是我需要的。我已经更新了我的问题。
      • 虽然理论上这可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
      • @GhostCat 我本来打算这样做,但以我的声誉无法做到这一点。如果我能够做到这一点,那么很难找到我可以按这种方式进行操作的盒子。但再次感谢您的提示
      • 嗯,你刚刚在这方面取得了一些进展。除此之外 - 省略诸如“谢谢”、“不客气”等内容。对于这个答案,我建议总结该页面 - 无需截图或显示图片。
      • @GhostCat 我一直在这个网站上寻找答案,我刚刚开始学习如何在这个网站上写作或回答问题。我看到人们说:“不客气”,所以我认为这是正常的做法。就像小孩子从父母那里学习言语和行为一样。
      猜你喜欢
      • 2012-08-03
      • 2020-04-10
      • 1970-01-01
      • 2021-08-04
      • 2012-05-31
      • 2012-11-27
      • 2017-11-21
      • 2015-08-25
      • 1970-01-01
      相关资源
      最近更新 更多