2013-06-05 51 views
2

我開發一個Android應用程序,我有一個問題:局部變量不可見裏面OnClickListener

我有這樣的方法:

// User has introduced an incorrect password. 
private void invalidPassword() 
{ 
    // R.id.string value for alert dialog title. 
    int dialogTitle = 0; 
    // R.id.string value for alert dialog message. 
    int dialogMessage = 0; 
    boolean hasReachedMaxAttempts; 

    clearWidgets(); 
    numIntents++; 
    hasReachedMaxAttempts = (numIntents > maxNumIntents); 

    // Max attempts reached 
    if (hasReachedMaxAttempts) 
    { 
     dialogTitle = R.string.dialog_title_error; 
     dialogMessage = R.string.dialog_message_max_attempts_reached; 
    } 
    else 
    { 
     dialogTitle = R.string.dialog_title_error; 
     dialogMessage = R.string.dialog_message_incorrect_password; 
    } 

    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage(dialogMessage) 
      .setTitle(dialogTitle); 
    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() 
    { 
      public void onClick(DialogInterface dialog, int id) 
      { 
       // TODO: User clicked OK button 
       if (hasReachedMaxAttempts) 
       { 
       } 
       else 
       { 
       } 
      } 
     }); 

    AlertDialog dialog = builder.create(); 
    dialog.show(); 
} 

我怎樣才能使人們看到裏面boolean hasReachedMaxAttempts;onClick

+0

將其聲明爲全局變量... –

+4

-2因爲我是Java編程的新手。這是如何獎勵想學習的人? – VansFannel

回答

4

你需要一個變量來最終決定;

final boolean hasReachedMaxAttemptsFinal = hasReachedMaxAttempts; 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 

if (hasReachedMaxAttemptsFinal) 
2

class level聲明你final boolean hasReachedMaxAttempts;變量,它應該得到完成任務

2

這是可見的,但它需要設置爲final

final boolean hasReachedMaxAttempts = (numIntents > maxNumIntents);