如何在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));
}
currentIntervalChoice在哪裏定義? – Egor
oops.sory我會編輯它。 – Manasi