2013-04-12 28 views
1

如何在onClick()外的onClick()中訪問用戶接受的值。我已經聲明瞭全局變量currentIntervalchoice,其中我正在回顧該值。如何在其他{}部分訪問currentIntervalchoice的值。alertDialog訪問onClick()內部的值

private void doFirstRun() 
{ 
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 
    if (settings.getBoolean("isFirstRun", true)) 
    { 
     //------------------------------------------------------------------------- 
     Toast.makeText(getApplicationContext(),"in 1st run true", Toast.LENGTH_LONG).show(); 
     LayoutInflater li = LayoutInflater.from(this); 
     View promptsView = li.inflate(R.layout.prompts, null); 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
     // set prompts.xml to alertdialog builder 
     alertDialogBuilder.setView(promptsView); 
     final EditText userInput = (EditText) promptsView 
       .findViewById(R.id.editTextDialogUserInput); 

     // set dialog message 
     alertDialogBuilder.setCancelable(false).setPositiveButton("OK", 
       new DialogInterface.OnClickListener() 
       { 
      public void onClick(DialogInterface dialog,int which) 
      { 
       String value = userInput.getText().toString(); 
       currentIntervalChoice=Integer.parseInt(value); 
       toggleLogging(AppSettings.getServiceRunning(MainActivity.this), 
         AppSettings.setLoggingInterval(MainActivity.this,currentIntervalChoice)); 
       dialog.dismiss(); 
       // return; 
      } 
       }); 
     // create alert dialog 
     AlertDialog alertDialog = alertDialogBuilder.create(); 
     // show it 
     alertDialog.show(); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putBoolean("isFirstRun", false); 
     editor.commit(); 
    } 
    else 
    { 

     Toast.makeText(getApplicationContext(),"in 1st run false", Toast.LENGTH_LONG).show();  
     toggleLogging(AppSettings.getServiceRunning(MainActivity.this), 
       AppSettings.setLoggingInterval(MainActivity.this,currentIntervalChoice)); 
    } 
+1

currentIntervalChoice在哪裏定義? – Egor

+0

oops.sory我會編輯它。 – Manasi

回答

2

從我看到的,你使用共享首選項來存儲/檢查這是否是第一次運行的應用程序。您的else零件不會在同一次運行中執行(因爲它是首次運行或不運行)。您應該做的是將用戶輸入的值存儲到共享首選項中,與存儲isFirstRun標誌的方式完全相同。然後在您的else部分中,只需從您的共享首選項中讀取該值即可。就像這樣:

final SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 
if (settings.getBoolean("isFirstRun", true)) 
{ 
    ... 

    alertDialogBuilder.setCancelable(false).setPositiveButton("OK", 
      new DialogInterface.OnClickListener() 
      { 
     public void onClick(DialogInterface dialog,int which) 
     { 
      String value = userInput.getText().toString(); 
      int currentIntervalChoice=Integer.parseInt(value); 
      Editor edit = settings.edit(); 
      editor.putBoolean("isFirstRun", false); 
      editor.putInt("currentIntervalChoice", currentIntervalChoice); 
      editor.commit(); 

      ... 
     } 
      }); 
    // create alert dialog 
    AlertDialog alertDialog = alertDialogBuilder.create(); 
    // show it 
    alertDialog.show(); 
} 
else 
{ 
    int currentIntervalChoice = settings.getInt("currentIntervalChoice", 0); 

    ... 
} 

請注意,我刪除了無關的代碼 - 您可能需要保留它。我還將您的isFirstRun標誌存儲在onClick之內。您可能還想對此值添加一些驗證,但這取決於您的邏輯。

+0

你可以發表一些代碼嗎?我不喜歡它。 – Manasi

+0

@Manasi我更新了我的答案 –

+0

ThankYou Aleks。我會試試這個。 – Manasi