我有我想要启动应用程序在第一次之后,显示仅在第一次警报。
I have an alert that I want to display only the first time right after starting the application for the first time.
我怎么能这样做?
有几个方法可以做到这一点,但也许最简单的是只是为了检查一个标志在共享preferences对象并设定它一旦警报被示出。
There are a few ways to do this, but perhaps the easiest is just to check a flag in a SharedPreferences object and set it once the alert is shown.
Shared$p$pferences
类似于
public class MyActivity extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean dialogShown = settings.getBoolean("dialogShown", false);
if (!dialogShown) {
// AlertDialog code here
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("dialogShown", true);
editor.commit();
}
}