android AlertDialog只显示一次
我只想在安装时显示一次对话框。我在currentIntervalChoice中接受用户的值。但在其他部分是无法访问的。我怎样才能做到这一点。我已在全球范围内宣布currentIntervalChoice。像这样android AlertDialog只显示一次,android,android-alertdialog,Android,Android Alertdialog,我只想在安装时显示一次对话框。我在currentIntervalChoice中接受用户的值。但在其他部分是无法访问的。我怎样才能做到这一点。我已在全球范围内宣布currentIntervalChoice。像这样 int currentIntervalChoice; 这是我的代码: private void doFirstRun() { SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRI
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));
}
}
我正在活动的onCreate()中调用doFirstRun()
谢谢你我用过这个
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();
使用SQLite。也许下次需要显示对话框。不清楚问题出在哪里;如果要保存该值,请使用
SharedReferences
或已使用共享首选项的活动中的私有变量。在else部分传递当前间隔的位置是否有任何错误?在else部分传递间隔值toggleLogging(AppSettings.getServiceRunning(MainActivity.this)、AppSettings.setLoggingInterval(MainActivity.this、currentIntervalChoice))代码>是的,我只在那里使用它。但是它没有从AlertDialogIt的onClick()中获取值,因为它工作正常。但问题是我无法在else部分访问currentIntervalChoice的值。toggleLogging(context,int)以interval=currentIntervalChoice启动我的服务