在我的Android应用程序中,我需要设计一个欢迎屏幕,只有在安装和打开应用程序后才会向用户显示一次.该应用程序是一个数据库驱动的应用程序,我希望包括一些3-4屏幕,以帮助用户创建可在应用程序中使用的可重用资源和一些提示.它们将是对话警报,最后一个欢迎屏幕显示"不再显示"复选框.
问题实际上是,如何只显示一次欢迎屏幕.任何帮助或指向该效果的指标都非常感谢.
Mar*_*tyn 35
这是我的应用程序中的一些代码.
在您的活动中:
SharedPreferences mPrefs;
final String welcomeScreenShownPref = "welcomeScreenShown";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// second argument is the default to use if the preference can't be found
Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);
if (!welcomeScreenShown) {
// here you can launch another activity if you like
// the code below will display a popup
String whatsNewTitle = getResources().getString(R.string.whatsNewTitle);
String whatsNewText = getResources().getString(R.string.whatsNewText);
new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(whatsNewTitle).setMessage(whatsNewText).setPositiveButton(
R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, true);
editor.commit(); // Very important to save the preference
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34148 次 |
| 最近记录: |