【问题标题】:How to set/update values of my preferences (PreferencesAcitivity)?Android如何设置/更新我的偏好值(PreferencesAcitivity)?Android
【发布时间】:2012-08-24 10:59:03
【问题描述】:

我有一个 SettingsActivity ( extends PreferenceActivity ) ,它是从一个preferences.xml 文件(res-->xml-->preferences.xml) 加载的。

preferences.xml 是这样的:

<?xml version="1.0" encoding="utf-8"?>

<PreferenceCategory android:title="Patient&apos;s Settings" >
    <EditTextPreference
        android:defaultValue="Not Set"
        android:key="patientMobile"
        android:title="mobile number" />
</PreferenceCategory>
<PreferenceCategory android:title="Doctor&apos;s Settings" >
    <EditTextPreference
        android:defaultValue="Not Set"
        android:key="docEmail"
        android:title="e-mail" />
    <EditTextPreference
        android:defaultValue="Not Set"
        android:key="docMobile"
        android:title="mobile number" />
</PreferenceCategory>
<PreferenceCategory android:title="Application Settings" >
    <SwitchPreference
        android:disableDependentsState="false"
        android:enabled="true"
        android:key="lang"
        android:summaryOff="English"
        android:summaryOn="Greek"
        android:switchTextOff="EN"
        android:switchTextOn="GR" />
</PreferenceCategory>

我如何从另一个活动中设置/更新/覆盖这些值?

我从 web 服务中检索信息,然后我想保存这些值,然后从 SettingsActivity 中看到:

1.patientMobile (string)
2.docEmail      (string)
3.docMobile     (string)

【问题讨论】:

    标签: android save sharedpreferences android-preferences


    【解决方案1】:

    您可以使用SharedPreferences 和首选项键读取/设置/更新这些值。

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    String patientMobile = preferences.getString("patientMobile");
    
    //or set the values. 
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("patientMobile", "yes"); //This is just an example, you could also put boolean, long, int or floats
    editor.commit();
    

    【讨论】:

    • 如果我在 MainActivity(我的菜单),我是怎么做的。作为上下文我把 SettingsActivity.class,比如:SharedPreferences 首选项 = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.class)?
    • 还有一个问题,如果 patientMobile 存在,它会覆盖它,如果它不存在,它会创建它?
    • 如果你在SettingsActivity(或任何ActivityService类,它们都继承自Context),你需要传递this作为上下文。至于你的第二个问题,答案是肯定的!
    • 我不在 SettingsActivity 但我想覆盖 MainActivity 中的一些值!那么?我把什么作为参数?
    • 正如我所说,任何Activity 类都可以。您的 MainActivity 扩展了 Activity 对吗?
    【解决方案2】:

    当您检索值时,您将它们保存为:

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(YourActivity.this);
    Editor editor = sp.edit();
    editor.putString("patientMobile", patientMobile);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD) {
        editor.apply();
    } else {
        editor.commit();
    }
    

    要读取值,只需调用sp.getString("patientMobile", defaultValue);

    apply()。还有SharedPreferences docs。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多