2011-06-29 51 views
2

我想使用preferences.xml文件来存储/检索应用程序范围的首选项。我在哪里存储XML文件,这样就可以使用:我在哪里把我偏好的XML文件在Android?

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")); 

} 

我的应用程序只是说“noth荷兰国际集团”。

谢谢。

回答

3

把它放在res/xml目录下。

+0

好的,我已经这么做了,新问题,请看上面的编辑。 –

+0

我正在关闭这个并开启一个新问题。感谢您的回答。 –

相关问题