【发布时间】:2011-09-25 13:26:12
【问题描述】:
我想使用一个preferences.xml 文件来存储/检索应用程序范围的首选项。我在哪里存储 xml 文件以便我可以使用:
getSharedPreferences("首选项", 0)
我的preferences.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory>
<Preference android:key="units_length" android:title="imperial" android:summary="Whatever"></Preference>
<Preference android:title="imperial" android:key="units_weight" android:summary="Whatever"></Preference>
</PreferenceCategory>
</PreferenceScreen>
这是 onCreate 方法在我的活动中的样子:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.results);
// Get preferences
preferences = getSharedPreferences("preferences", 0);
// Fetch the text view
TextView text = (TextView) findViewById(R.id.textView1);
// Set new text
text.setText(preferences.getString("units_length", "nothing"));
}
我的应用程序只是说“什么都没有”。
谢谢。
【问题讨论】:
标签: android preferences sharedpreferences