java - 第一次尝试尝试共同的偏好

标签 java android sharedpreferences

public class MainActivity extends Activity {
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textView);

        SharedPreferences sp = this.getPreferences( Context.MODE_PRIVATE );
        SharedPreferences.Editor editor = sp.edit();

        Random rand = new Random();

        int randomNum = rand.nextInt( 1000 );

        editor.putString("EQUATION_KEY_" + String.valueOf(randomNum), "");
        editor.commit();

        String str = sp.getString(String.valueOf(randomNum), "");

        textView.setText(str);
    }
}

我不知道为什么这不能仅在屏幕上显示保存的文本。 logcat 是这样说的:

02-18 10:11:24.988 3196-3196/? I/art: Not late-enabling -Xcheck:jni (already on)
02-18 10:11:25.169 3196-3196/com.example.luke.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.luke.myapplication-2/lib/x86
02-18 10:11:25.252 3196-3217/com.example.luke.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-18 10:11:25.430 3196-3217/com.example.luke.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
02-18 10:11:25.749 3196-3217/com.example.luke.myapplication W/EGL_emulation: eglSurfaceAttrib not implemented
02-18 10:11:25.750 3196-3217/com.example.luke.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xab78ee40, error=EGL_SUCCESS
02-18 10:11:38.677 3196-3202/com.example.luke.myapplication W/art: Suspending all threads took: 21.161ms

请帮助我,我是菜鸟,但想学习。

最佳答案

为您的应用程序创建通用类,请按照以下步骤操作:

第 1 步: 创建应用程序级别类

public class AppConfig extends Application {

private static AppConfig appInstance;
private static SharedPreferences sharedPreferences;
private static SharedPreferences.Editor sharedPreferencesEditor;
private static Context mContext;

@Override
public void onCreate()
{
    super.onCreate();
    appInstance = this;
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    sharedPreferencesEditor = sharedPreferences.edit();
    setContext(getApplicationContext());

}

public static Context getContext() {
    return mContext;
}

public static void setContext(Context mctx) {
    mContext = mctx;
}


public static AppConfig getAppInstance() {
    if (appInstance == null)
        throw new IllegalStateException("The application is not created yet!");
    return appInstance;
}

/**
 * Application level preference work.
 */
public static void preferencePutInteger(String key, int value) {
    sharedPreferencesEditor.putInt(key, value);
    sharedPreferencesEditor.commit();
}

public static int preferenceGetInteger(String key, int defaultValue) {
    return sharedPreferences.getInt(key, defaultValue);
}

public static void preferencePutBoolean(String key, boolean value) {
    sharedPreferencesEditor.putBoolean(key, value);
    sharedPreferencesEditor.commit();
}

public static boolean preferenceGetBoolean(String key, boolean defaultValue) {
    return sharedPreferences.getBoolean(key, defaultValue);
}

public static void preferencePutString(String key, String value) {
    sharedPreferencesEditor.putString(key, value);
    sharedPreferencesEditor.commit();
}

public static String preferenceGetString(String key, String defaultValue) {
    return sharedPreferences.getString(key, defaultValue);
}

public static void preferencePutLong(String key, long value) {
    sharedPreferencesEditor.putLong(key, value);
    sharedPreferencesEditor.commit();
}

public static long preferenceGetLong(String key, long defaultValue) {
    return sharedPreferences.getLong(key, defaultValue);
}

public static void preferenceRemoveKey(String key) {
    sharedPreferencesEditor.remove(key);
    sharedPreferencesEditor.commit();
}

public static void clearPreference() {
    sharedPreferencesEditor.clear();
    sharedPreferencesEditor.commit();
}

}

第 2 步:将此类定义到 Application 标记中的 Manifest.xml 中,如下所示

<application
    android:name=".AppConfig">
 </application>

第 3 步: 您可以在您的 Activity 中使用以下代码

AppConfig.preferencePutInteger("Key",Value);
AppConfig.preferenceGetInteger("Key", 0)

完成快乐编码......:)我希望你理解。

关于java - 第一次尝试尝试共同的偏好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35489346/

相关文章:

java-根据指定的整数值对字符串的链接列表进行排序

Java 日志文件读取器,charAt() 字符串索引超出范围

android - gradle 发现版本错误

android - 如何导航 map (Android Maps API v2)

java - 如何检查应用程序是否正在打开 - Android Studio

java - 正则表达式将空格和单词替换为单词的 toFirstUpper

java - 将 Scala 编织到现有的 Java EE 项目中?

Android studio 模拟器 sqlite db 文件

android - 除非滚动,否则 ListView 图像不会改变

flutter - 如何使用 SharedPreferences 保存和检索自定义对象列表?