我在哪里将我的偏好xml文件放在Android中?

时间:2011-06-29 22:26:29

标签: android preferences sharedpreferences

我想使用preferences.xml文件来存储/检索应用程序范围的首选项。我在哪里存储xml文件以便我可以使用:

  

getSharedPreferences(“preferences”,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"));

}

我的申请表示“没有”。

感谢。

1 个答案:

答案 0 :(得分:3)

将它放在res / xml目录中。