2012-03-13 131 views
2

的方法我有一個方法:打破了由匿名內部類

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()返回一個布爾值,然後從負按鈕返回,但它不會讓我,因爲這是一個無效的內部方法中。
  • 拋出異常ESCAPE被外部抓到,但編譯器不會讓我因爲DialogInterface.OnClickListener不拋出。使用break聲明離開for循環,但這也不起作用。

簡單地離開for循環也是可以接受的。我可以解釋這一點。我試過了我能找到的所有東西,而且我在智慧的結尾。

回答

3

您可以拋出RuntimeException或其一個子類。編譯器不會抱怨它。

1

你可以提高你的代碼:

// 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有這樣做太像處理程序等

+0

根據您的建議,是否可以在'someMethod'中聲明'shouldCancel'作爲變量?還是必須在一個單獨的課堂?另外,'Handler'怎麼會比AlertDialog更受歡迎呢? – gobernador 2012-03-13 04:43:56

+0

爲了回答我自己的問題,'shouldCancel'必須封裝在類中。這允許我在匿名內部類中修改它。 – gobernador 2012-06-04 17:07:37

1

的其他方法你是不是在循環,方法執行時。它可以訪問在那裏聲明的變量(如果它們是最終的),但是OnClickListener在它們單擊之後執行,完全在該循環之外/從該循環移除。

0

要突破任何方法,即使它位於匿名內部類中,也可以使用return關鍵字。

如果您使用的方法如someMethod,則只需使用return;如果您的方法返回對象,請使用return null;。如果它返回short,int,double,floatlong,請使用return 0;。如果返回char,使用return Character.MIN_VALUE;對於booleanreturn false;

好了,你的想法。我希望這篇文章能幫助別人。