4

我想在共享首选项中保存和获取雪密度的静态整数值,并在用户在单一选项中选择另一个值时进行更改。
我的代码:

public static int mSnowDensity;
AlertDialog.Builder mABuilder = new AlertDialog.Builder(AAA.this);
final CharSequence mCharSequence[] = { "Low", "Medium", "High" };
mABuilder.setTitle("Set Density of Snow");
mABuilder.setSingleChoiceItems(mCharSequence,
        WallpaperServices.mDensitySnow,
        new android.content.DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                if (which == 2) {

                    mSnowDensity = 90;
             /*I Want to save mSnowDensity Value In Shared Preferences */
                } else if (which == 1) {

                     mSnowDensity = 60;
                } else {

                     mSnowDensity = 30;
                }

                dialog.dismiss();
            }
        });
4

5 回答 5

15

您可以按如下方式使用共享首选项

//To save
SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("SNOW_DENSITY",mSnowDensity);
editor.commit();

//To retrieve
SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
int snowDensity = settings.getInt("SNOW_DENSITY", 0); //0 is the default value

getSharedPreferences() 是 Context 类的方法。如果您在 Activity 或 Service(扩展 Context)中,则可以像在此代码段中一样使用它。否则,您应该使用 getApplicationContext() 获取上下文,然后调用 getSharedPreferences() 方法。

有关更多选项,您可以参考http://developer.android.com/guide/topics/data/data-storage.html#pref上的文档

于 2013-07-29T05:58:53.180 回答
1

要保存在 SharedPreferences 中:

private final String PREFS_NAME  = "filename";
private final String KEY_DENSITY    = "den";

Context  ctx   = getApplicationContext();
SharedPreferences sharedPreferences = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();

editor.putInt(KEY_DENSITY, mSnowDensity);
editor.commit();

要获取值:

Context ctx = getApplicationContext();
String strSavedValue = null;
SharedPreferences sharedPreferences = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

strSavedValue = sharedPreferences.getInt("den", anyDefaultValue);
于 2013-07-29T05:59:39.577 回答
1

将值保存在优先级中

private void SavePreferences(String key, int value) {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

从偏好中获取价值

 private void showPreferences(String key){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        int savedPref = sharedPreferences.getInt(key, 0);

       }

您可以将密钥用作共享首选项名称

于 2013-07-29T06:01:13.960 回答
1

初始化 我们需要一个编辑器来编辑和保存共享首选项中的更改。以下代码可用于获取共享首选项。

 SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 :- for private mode
Editor editor = pref.edit();

存储数据

editor.putBoolean("key_name", true); 
editor.putString("key_name", "string value"); 
editor.putInt("key_name", "int value"); 
editor.putFloat("key_name", "float value");
editor.putLong("key_name", "long value");  
editor.commit(); 

检索数据

pref.getString("key_name", null); // getting String
pref.getInt("key_name", -1); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean

清除或删除数据

editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email

editor.commit(); // commit changes


editor.clear();
editor.commit(); // commit changes
于 2018-12-08T21:00:49.630 回答
0

保存偏好并不是一项麻烦的任务。但是如果你有很多这样的可配置选项,你可以使用 aPreferenceActivity和 override onSharedPreferenceChanged

更多细节在这里http://developer.android.com/guide/topics/ui/settings.html

于 2013-07-29T06:09:26.220 回答