-1

我开发安卓应用。我希望我的应用程序在首次安装时有指南。当我按下“开始应用程序”按钮时,该指南将消失。当应用程序返回本地启动时,该指南将不会重新出现。谢谢之前 :D

4

2 回答 2

1

使用共享首选项,请参阅我的回答:查看页面指示器中的共享首选项

当用户按下按钮时存储一个布尔值,如果设置了布尔值,您可以跳过指南。

将在应用程序安装的整个生命周期内工作。

首先创建 PreferencesData 类(保留链接中的 String 方法并添加布尔方法)

public class PreferencesData {

    public static void saveString(Context context, String key, String value) {
    SharedPreferences sharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(context);
    sharedPrefs.edit().putString(key, value).commit();
    }

    public static String getString(Context context, String key, String defaultValue) {
    SharedPreferences sharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(context);
    return sharedPrefs.getString(key, defaultValue);
    }


    public static void saveBoolean(Context context, String key, Boolean value) {
    SharedPreferences sharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(context);
    sharedPrefs.edit().putBoolean(key, value).commit();
    }

    public static Boolean getBoolean(Context context, String key, Boolean defaultValue) {
    SharedPreferences sharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(context);
    return sharedPrefs.getBoolean(key, defaultValue);
    }
}

现在,在您的 MainActivity 中(我假设您在这里有一个 MainActivity 和一个 GuideActivity)

  protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     // will return the default value true if never been set before
     if (PreferencesData.getBoolean(this, "showGuide", true) {
         startActivity(new Intent(MainActivity.this, GuideActivity.class));

         // you can do this from the last step of your guide instead
         // to make sure that the guide is shown again if user 
         // quit before completing it

         PreferencesData.saveBoolean(this, "showGuide", false);

         finish();
     } else {
          // continue application
          setContentView(R.id.yourlayout);
          ...
     }
 }
于 2013-10-02T01:54:15.103 回答
0

将演练指南设置为应用程序的主要活动,但在为该活动实例化任何布局之前,请检查是否存在一个标志,该标志表明该指南之前是否已被关闭一次。此标志需要在应用程序关闭时保留在某个位置 - 在属性文件或数据库中。用户第一次关闭指南时,将该标志设置为 true;下次您在启动时检查它时,您将能够跳过演练的布局并将您的应用程序直接发送到它的第一个“真实”活动。

编辑: 被打败了。是的 - 使用 SharedPreferences 除非您已经有一个您希望使用的数据库设置。

于 2013-10-02T01:57:38.283 回答