【问题标题】:How to display a one time welcome screen?如何显示一次性欢迎屏幕?
【发布时间】:2010-10-20 09:29:44
【问题描述】:

在我的 android 应用程序中,我需要设计一个欢迎屏幕,在安装并打开应用程序后只向用户显示一次。有问题的应用程序是一个数据库驱动的应用程序,我很想包括一些 3 - 4 个屏幕来帮助用户创建可在应用程序中使用的可重用资源和一些提示。它们将是对话框警报,最后一个欢迎屏幕显示“不再显示”复选框。

真正的问题是,如何只显示一次欢迎屏幕。非常感谢任何有关此效果的帮助或指示。

【问题讨论】:

    标签: android


    【解决方案1】:

    这是我的应用程序中的一些代码。

    在你的活动中:

    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
        }
    
    }
    

    【讨论】:

    • 这可能比我想象的要好。只是一个简单的问题。什么是版本号?
    • @Siddharth:我猜所以更新应用程序后消息仍然显示(一次)。
    • 啊..好的,抱歉 - 我没有正确清除此代码。此代码将检查存储的版本号并弹出消息是应用程序的当前版本号不同。对于上下文,当用户升级应用程序时,我使用它来弹出一条消息 - 如果你愿意,我可以为你发布正确的代码?
    • 我的意见?如果要在每次升级后显示消息,请使用此选项。如果没有,请使用我的建议。 (和这个基本一样,少了几行。)
    • @Martyn:我很想看到正确的代码。对于像我这样的业余爱好者来说,即使是来自像你这样有经验的人的一些精炼代码的一部分也非常棒。 :)
    【解决方案2】:

    在完成欢迎屏幕内容后,在启动应用程序时在首选项中保存一个标志。在显示欢迎屏幕之前检查此标志。如果标志存在(换句话说,如果不是第一次),不要显示它。

    【讨论】:

    • 那么像 Android Preferences 中的 Preferences?这可能就是我正在寻找的。会试一试。
    • 是的,我就是这个意思。我不知道这是否是 Android 上的“它是如何完成的”,但我就是这样做的。 :)
    • 偏好?我应该为我注意到它。我们现在通过将一些设置存储在文件中来做到这一点。
    • 我认为你已经为我设定了正确的方向。完成后将发布 cmets。谢谢一堆。 :)
    • 就是这样。这就像一个魅力。感谢所有做出贡献的人。 :)
    【解决方案3】:

    我用这个创建了一个 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 秒 } };

    【讨论】:

    • 这真的不是闪屏。当用户第一次安装和运行应用程序时,我需要显示一些提示和提示。更像是教程和演练。感谢您的帮助..... :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多