Save preference settings for users in the Android app (Chuanzhi podcast video notes)

Source: Internet
Author: User

The XML file generated by the application is saved in/data/application package name/shared_prefs/name. xml.

(/Data/Data/COM. sinaapp. Shared/Shared_prefs/ssun. XML)

Sharedpreferenceactivity. Java source code:

Package COM. sinaapp. shared; </P> <p> Import Java. util. map; </P> <p> Import COM. sinaapp. service. service; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. edittext; <br/> Import android. widget. toast; </P> <p> public class sharedpreferenceactivity extends activity {<br/>/** called when Activity is first created. */<br/> edittext nametext; <br/> edittext agetext; <br/> Service; <br/> Map <string, string> map; </P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> nametext = (edittext) This. findviewbyid (R. id. nametext); <br/> agetext = (edittext) This. findviewbyid (R. id. agetext); <Br/> service = new service (this); <br/> map = service. getpreference (); <br/> nametext. settext (map. get ("name"); <br/> agetext. settext (map. get ("Age"); <br/>}</P> <p> Public void save (view v) {<br/> string name = nametext. gettext (). tostring (); <br/> int age = integer. parseint (agetext. gettext (). tostring (); <br/> service. save (name, age); <br/> toast. maketext (getapplicationcontext (), "Save success! ", Toast. length_long). Show (); <br/>}< br/>}
Service. Java source code:

Package COM. sinaapp. service; </P> <p> Import Java. util. hashmap; <br/> Import Java. util. map; </P> <p> Import android. content. context; <br/> Import android. content. sharedpreferences; <br/> Import android. content. sharedpreferences. editor; </P> <p> public class service {<br/> private context; </P> <p> Public Service (context) {<br/> This. context = context; <br/>}</P> <p>/** <br/> * Save the parameter <br/> * @ Param name <br/> * @ Param age <br /> */<br/> Public void save (string name, int age) {<br/> sharedpreferences preferences = context. getsharedpreferences ("ssun", context. mode_private); <br/> editor edit = preferences. edit (); <br/> edit. putstring ("name", name); <br/> edit. putint ("Age", age); <br/> edit. commit (); <br/>}</P> <p>/** <br/> * @ return parameters previously set <br/> */<br/> Public Map <string, string> getpreference () {<br/> sharedpreferences preferences = context. getsharedpreferences ("ssun", context. mode_private); <br/> Map <string, string> map = new hashmap <string, string> (); <br/> map. put ("name", preferences. getstring ("name", ""); <br/> map. put ("Age", String. valueof (preferences. getint ("Age", 0); <br/> return map; <br/>}< br/>
Main. xml file:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Orientation = "vertical"> </P> <p> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/Name"/> </P> <p> <edittext <br/> Android: Id = "@ + ID/nametext" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: inputtype = "text" <br/> Android: EMS = "10"> </P> <p> <requestfocus/> <br/> </edittext> </P> <p> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: TEXT = "@ string/age"/> </P> <p> <edittext <br/> Android: Id = "@ + ID/agetext" <br/> Android: layout_width = "match_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: inputtype = "Number" <br/> Android: EMS = "10"/> </P> <p> <button <br/> Android: Id = "@ + ID/button1" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "@ string/Save" <br/> Android: onclick = "save"/> </P> <p> </linearlayout>
Strings. xml file:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> </P> <p> <string name = "name"> name </string> <br/> <string name = "Age"> Age </string> <br/> <string name = "app_name"> USER parameter settings </string> <br/> <string name = "save"> Save </string> <br/> </resources>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.