alertDialog访问onClick()里面的值

时间:2013-04-12 10:02:38

标签: android android-alertdialog

如何在onClick()之外的onClick()中访问用户接受的值。我已经全局声明了变量currentIntervalchoice,我正在重新获取该值。如何在else {} part。中访问currentIntervalchoice的值。

    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 个答案:

答案 0 :(得分: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内。您可能还想对此值添加一些验证,但这取决于您的逻辑。