我已阅读 here ,首选项会自动保留。我尝试按照说明进行操作,但该复选框始终具有默认值。我的代码如下:
1) 我有一个可打开“首选项”屏幕的菜单:
int id = item.getItemId();
if (id == R.id.action_settings) {
{
Intent myIntent = new Intent(this, PreferencesActivity.class);
startActivity(myIntent);
}
2) Preferences Fragment 类,加载设置:
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_screen);
}
}
3) 加载 fragment 的 Activity :
public class PreferencesActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
首选项屏幕 xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference android:enabled="true"
android:title="@string/ShowAdsTitle"
android:summaryOn="@string/ShowAdsOnPreference"
android:selectable="true"
android:summaryOff="@string/ShowAdsOnPreference"
android:key="@string/value_showads_preference"/>
</PreferenceScreen>
不知何故,我的设置不是持久的。 android:key 是为我的每个设置设置的。
提前致谢!
最佳答案
android:key="value"/>
尝试为 android:key 放置一个直接字符串,而不是字符串中的值,因为您将无法访问翻译后的键值
关于java - Android 中的首选项不会保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24849718/