【问题标题】:Break out of a method from anonymous inner class从匿名内部类中跳出一个方法
【发布时间】:2012-03-13 04:28:41
【问题描述】:

我有一个方法:

void someMethod(String someString)
    final String[] testAgainst = {...};
    ....
    for(int i = 0; i < testAgainst.length; i++) {
        if (someString.equals(testAgainst[i])) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Strings are the same! Overwrite?")
                   .setTitle("Blah Blah Blah")
                   .setCancelable(false)
                   .setPositiveButton("Overwrite", new DialogInterface.OnClickListener() {

                       public void onClick(DialogInterface di, int which) {
                           someAction()
                       }
                   })

                   .setNegativeButton("Nah", new DialogInterface.OnClickListener() {

                       public void onClick(DialogInterface di, int which) {
                           ESCAPE
                       }
                   });
            AlertDialog dialog = builder.create();
        }
    }

    doSomeOtherStuff();
}

事情是这样的,如果我的代码达到ESCAPE(即用户决定不覆盖),我想完全退出该方法。我试过了……

  • someMethod() 更改为返回一个布尔值,然后从否定按钮返回它,但它不会让我这样做,因为它在一个 void 内部方法中。
  • ESCAPE 抛出异常以被外部捕获,但编译器不允许我这样做,因为DialogInterface.OnClickListener 不会抛出。
  • 使用break 语句离开for 循环,但这也不起作用。

简单地离开for 循环也是可以接受的。我可以解释这一点。我已经尝试了所有能找到的方法,但我已经束手无策了。

【问题讨论】:

    标签: java android anonymous-inner-class


    【解决方案1】:

    您可以抛出 RuntimeException 或其子类之一。编译器不会抱怨。

    【讨论】:

      【解决方案2】:

      当该方法执行时,您不在循环中。它可能可以访问那里声明的变量(如果它们是最终的),但 OnClickListener 一旦单击就会执行,完全在该循环之外/从该循环中删除。

      【讨论】:

        【解决方案3】:

        您可以通过以下方式增强您的代码:

        // Static class that contains nothing but a trigger to exit the loop
        static class Cancel { boolean shouldCancel = false; }
        
        void someMethod(String someString)
            final String[] testAgainst = {...};
            ....
        
            // Initialize it `final`, else it won't be accessible inside
            final Cancel trigger = new Cancel();
        
            // Add the check as additional condition for the `for` condition
            for(int i = 0; i < testAgainst.length && !trigger.shouldCancel; i++) {
                if (someString.equals(testAgainst[i])) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("Strings are the same! Overwrite?")
                           .setTitle("Blah Blah Blah")
                           .setCancelable(false)
                           .setPositiveButton("Overwrite", new DialogInterface.OnClickListener() {
        
                               public void onClick(DialogInterface di, int which) {
                                   someAction()
                               }
        
                           .setNegativeButton("Nah", new DialongInterface.OnClickListener() {
        
                               public void onClick(DialogInterface di, int which) {
                                   // Use the trigger to communicate back that it's time to finish
                                   trigger.shouldCancel = true;
                               }
                    AlertDialog dialog = builder.create();
                }
            }
        
            doSomeOtherStuff();
        }
        

        Android 也有其他方法可以做到这一点,例如处理程序等。

        【讨论】:

        • 根据您的建议,是否可以将shouldCancel 声明为someMethod 中的变量?还是必须在一个单独的班级中?另外,Handler 如何优于AlertDialog
        • 要回答我自己的问题,shouldCancel 必须封装在类中。这允许我在匿名内部类中修改它。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-05
        • 1970-01-01
        • 1970-01-01
        • 2013-10-19
        相关资源
        最近更新 更多