如何显示一次性欢迎屏幕?

时间:2010-10-20 09:29:44

标签: android

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

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

3 个答案:

答案 0 :(得分: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
    }

}

答案 1 :(得分:23)

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

答案 2 :(得分:2)

我用这个创建了一个SplashScreen:

package com.olidroide;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


public class SplashScreen extends Activity{
/** Called when the activity is first created. */
     public ProgressDialog myDialog; 

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);

        new Handler().postDelayed(new Runnable() {

            public void run() { 
                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 Seconds
    }
};
相关问题