小编Ala*_*tos的帖子

如何在 sharedPreferences 中保存字符串

我有一个应用程序,我需要在共享首选项中保存一个字符串,以便用户已经打开应用程序一次并注册了他的电子邮件,他不必再次通过同一屏幕,而是直接转到主屏幕。

我的班级首选项助手

public class PreferencesHelpers {

private static final String SHARED_PREFS = "sharedPrefs";
private static final String TEXT = "ahhsaushhuuashu"; //I want to save this string

public String text;


public static void saveData(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putString("", TEXT);

}

public static String loadData(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    String text = sharedPreferences.getString("ahhsaushhuuashu", "");
    return text;
   }

}
Run Code Online (Sandbox Code Playgroud)

MainActivity 中的 MyLogic 用于保存和检索 sharedPreferences 值

  if (!preferencesHelpers.loadData(getApplicationContext()).contains("ahhsaushhuuashu")) {
                webView.loadUrl(URL_);
                preferencesHelpers.saveData(getApplicationContext());
            } else …
Run Code Online (Sandbox Code Playgroud)

java android sharedpreferences

-1
推荐指数
1
解决办法
4578
查看次数

标签 统计

android ×1

java ×1

sharedpreferences ×1