尝试首次尝试共享首选项

时间:2016-02-18 18:16:48

标签: 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

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

2 个答案:

答案 0 :(得分:1)

在这行代码中,您不是将randomNum用作商店值,而是将其附加到存储密钥。

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

应该是

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

答案 1 :(得分:1)

为您的应用程序创建公共类请遵循以下步骤:

第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步: 您可以将以下代码用于您的活动

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

完成快乐编码.... :)希望你明白。