如何从警报中返回值?

时间:2017-09-05 22:34:27

标签: return java-me alert display

我试图实现一种方法,其目的是在继续执行某些潜在的破坏性操作之前获得用户的确认。我所追求的是一种方法,它使用是/否选择发出警报,并将选择返回到称为它的任何代码段。相关代码部分如下:

<CODE>
// In the main MIDlet
//
    public void commandAction(Command c, Displayable d) {
        String s = "";
        boolean b = true;
        char k;

        kommand = c.getLabel();
//
// ... handlers for other commands
//
        if (c == CMD_DELETE) {
            if (current_name != "/") {
                kommand = "Delete";
                k = getConfirmation();
                /****** DON'T DELETE ANYTHING YET!!!!!!
                try {
                    current_fc.setFileConnection(s);
                    current_fc.dele;te();
                    current_fc.setFileConnection("..");
                } catch (Exception e) {}
                ****** We need to write the confirmation getter first! ****/;
            }
            showDirs();
        }

    }    // End of command actions


// The confirmation getter
// I've also tried declaring this method as a boolean
//
    public char getConfirmation() {
        char k = '?';
        Alert ExitAlrt = new Alert("Confirmation Required",
                                       "Are you sure?", null, AlertType.WARNING);
        ExitAlrt.addCommand(CMD_YES); 
        ExitAlrt.addCommand(CMD_NO);
        ExitAlrt.setCommandListener(new CommandListener() {
            public void commandAction(Command c, Displayable d) {
                if (c == CMD_YES) {
                    kommand = kommand + "Y";
                } else {
                    kommand = kommand + "N";
                }
                tick.setString(kommand);
                mainDisplay.setCurrent(flist);
                notify();
            }
        }  );
// This section of code results in generating an extra class
// file with the same name as the main class file but with a
// $1 appended.             

        mainDisplay.setCurrent(ExitAlrt);
        try {
            wait();
        } catch (InterruptedException ie) {}
        k = kommand.charAt(kommand.length()-1);
        return k;
    }
</CODE>

这个版本能让我得到最接近我想要的结果。尽管如此,我还能够从自动收录器中显示的内容以及何时,程序执行没有返回到调用getConfirmation()的位置。相反,它似乎正在跳过删除命令的其余部分,如果阻止并返回主CommandListener。

我最初的尝试方式与在C中完成的方式相同:没有notify()和try-wait-catch。该版本在屏幕上闪烁了大约十分之一秒的警报,然后又返回主显示屏。 添加try-wait-catch允许警报保留在屏幕上,但是当我做出选择时显示器不会切换回来。添加notify()允许第一眼看起来像期望的行为,除了应该在自动收报机中显示的内容不会。 (showDirs()方法调用另一个方法,将其他信息放入自动收报机。)

这究竟是怎么做的?

0 个答案:

没有答案