As you've seen in the previous section, Preferenceactivity This class allows users to create and modify preference information at run time. If you want to use these preference information, you need to use the Sharedpreferences class.
1. Continue to use the works in the previous section to add some code to the Usingpreferences.java.
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);
DisplayText (appprefs.getstring ("Edittextpref", ""));
public void onclickmodify (view view) {Sharedpreferences Appprefs = Getsharedpreferences ("Net.manoel.UsingPreferences_preferences", 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 (); }
}
2. Press F11 to debug on the simulator. This time click Display Preferences Values This button, you will see: