1

如何在 onClick() 之外的 onClick() 中访问用户接受的值。我已经在全局范围内声明了变量 currentIntervalchoice,我将在其中检索值。如何在 else{} 部分访问 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));
    }
4

1 回答 1

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. 您可能还想对该值添加一些验证,但这取决于您的逻辑。

于 2013-04-12T10:06:53.750 回答