On my device, the default preference name is Net.manoel.UsingPreferences_preferences.xml, which is prefixed with the name of the package. However, it is also useful to define a special name for a preference item. You can do this in the following way.
public class Apppreferenceactivity extends Preferenceactivity {
@Override public
void OnCreate (Bundle Savedinstancestate) {
super.oncreate (savedinstancestate);
Preferencemanager prefmgr = Getpreferencemanager ();
Prefmgr.setsharedpreferencesname ("apppreferences");
---Load the preferences from a XML file---
addpreferencesfromresource (r.xml.myapppreferences);
}
}
Here, we use the Preferencemanager class to set the preference file's name as Apppreferences.xml.
At the same time, modify Usingpreferencesactivity.java this class.
The public class Usingpreferencesactivity extends activity {/** called the "when" is the "
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main); public void Onclickload (view view) {Intent i = new Intent ("Net.manoel.AppPreferenceActi
Vity ");
StartActivity (i);
public void Onclickdisplay (view view) {/* Sharedpreferences appprefs = Getsharedpreferences ("Net.manoel.UsingPreferences_preferences", mode_private
);
* * Sharedpreferences appprefs = getsharedpreferences ("apppreferences", mode_private);
DisplayText (appprefs.getstring ("Edittextpref", ""));
public void onclickmodify (view view) {/* Sharedpreferences appprefs = getsharedpreferences ("Net.manoel.UsingPreferences_prefere
NCEs ", mode_private);
* * Sharedpreferences appprefs = getsharedpreferences ("apppreferences", mode_private);
Sharedpreferences.editor prefseditor = Appprefs.edit (); Prefseditor.putstring ("Edittextpref", (edittext) Findviewbyid (r.id.txtstring)). GetText (). ToString ()
);
Prefseditor.commit (); } private void DisplayText (String str) {Toast.maketext (Getbasecontext (), str, toast.lengt
H_long). Show (); }
}
When we return to the application and modify the preferences, we see that the Apppreferences.xml file has been created.