【发布时间】:2010-10-20 09:29:44
【问题描述】:
在我的 android 应用程序中,我需要设计一个欢迎屏幕,在安装并打开应用程序后只向用户显示一次。有问题的应用程序是一个数据库驱动的应用程序,我很想包括一些 3 - 4 个屏幕来帮助用户创建可在应用程序中使用的可重用资源和一些提示。它们将是对话框警报,最后一个欢迎屏幕显示“不再显示”复选框。
真正的问题是,如何只显示一次欢迎屏幕。非常感谢任何有关此效果的帮助或指示。
【问题讨论】:
标签: android
在我的 android 应用程序中,我需要设计一个欢迎屏幕,在安装并打开应用程序后只向用户显示一次。有问题的应用程序是一个数据库驱动的应用程序,我很想包括一些 3 - 4 个屏幕来帮助用户创建可在应用程序中使用的可重用资源和一些提示。它们将是对话框警报,最后一个欢迎屏幕显示“不再显示”复选框。
真正的问题是,如何只显示一次欢迎屏幕。非常感谢任何有关此效果的帮助或指示。
【问题讨论】:
标签: android
这是我的应用程序中的一些代码。
在你的活动中:
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
}
}
【讨论】:
在完成欢迎屏幕内容后,在启动应用程序时在首选项中保存一个标志。在显示欢迎屏幕之前检查此标志。如果标志存在(换句话说,如果不是第一次),不要显示它。
【讨论】:
我用这个创建了一个 SplashScreen:
包 com.olidroide; 导入android.app.Activity; 导入 android.app.ProgressDialog; 导入android.content.Intent; 导入android.os.Bundle; 导入android.os.Handler; 导入android.os.Message; 公共类 SplashScreen 扩展 Activity{ /** 在第一次创建活动时调用。 */ 公共 ProgressDialog myDialog; @覆盖 公共无效 onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splashscreen); 新的 Handler().postDelayed(新的 Runnable() { 公共无效运行(){ myDialog = ProgressDialog.show(SplashScreen.this,"", "Loading", true); Intent intent=new Intent(SplashScreen.this,OtherActivity.class); SplashScreen.this.startActivity(intent); myDialog.dismiss(); SplashScreen.this.finish(); } }, 3000);// 3 秒 } };【讨论】: