0

我只想在安装时显示一次对话框。我在 currentIntervalChoice 中接受用户的价值。但在其他部分无法访问。我怎样才能做到这一点。我已经在全球范围内声明了 currentIntervalChoice。像这样。

   int 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));
    }
}

我在我的 Activity 的 onCreate() 中调用 doFirstRun()。

谢谢你

4

2 回答 2

0

我用这个

boolean disclaimer = getSharedPreferences(PREF_NAME, 0).getBoolean(DISCLAIMER, false);
                if(!disclaimer)
                    {
                    startActivity(new Intent(SplashScreenActivity.this,DisclaimerActivity.class));
                    }else{                  
                        startActivity(new Intent(SplashScreenActivity.this,MainActivity.class));
                    }

并在免责声明活动中更新了 pref 值

public void onAccept(View v) {
    Editor edit = getSharedPreferences(PREF_NAME, 0).edit();
    edit.putBoolean(DISCLAIMER, true);
    edit.commit();
于 2013-04-12T07:43:23.910 回答
0

使用 SQLite。也许下次需要显示对话框。

于 2013-04-12T09:33:37.713 回答