Use of Android sharedpreference

Source: Internet
Author: User
Tags gettext

In the article "Android in the internal storage read and write files," the way to log in the user name and password, by reading the memory file to implement, but there is a problem, because the login name and password form through the username# #password的形式, read by the # #为分割线, However, once the # #就会出现问题 exists in the username, this article uses sharedpreference to solve the problem

1. Defining layouts
Use the previous layout
2. Save the state of the component with Sharedpreference

* Write data in sharedpreference

Got a Sharedpreference object.
Sharedpreferences sp = getsharedpreferences ("config", mode_private);
Get the editor
Editor ed = Sp.edit ();
Ed.putboolean ("checkbox", isChecked);
Ed.commit ();

* Collect data from sharedpreference

Sharedpreferences sp = getsharedpreferences ("config", mode_private);
Fetch data from Sharedpreference.
Boolean B = Sp.getboolean ("checkbox", false);

The simplified code is as follows:

 Packagecom.wuyudong.sharedpreference;ImportAndroid.os.Bundle;Importandroid.app.Activity;Importandroid.content.SharedPreferences;ImportAndroid.content.SharedPreferences.Editor;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.EditText; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //get to Sharedpreference objectSharedpreferences sp = getsharedpreferences ("info", mode_private); //get the contents of the SPString name = sp.getstring ("name", "" "); String Pass= sp.getstring ("Pass", "" "); EditText Et_name=(EditText) Findviewbyid (r.id.et_name); EditText Et_pass=(EditText) Findviewbyid (R.id.et_pass); //data echo to input boxEt_name.settext (name);    Et_pass.settext (pass); }     Public voidClick (View v) {EditText et_name=(EditText) Findviewbyid (r.id.et_name); EditText Et_pass=(EditText) Findviewbyid (R.id.et_pass); String name=Et_name.gettext (). toString (); String Pass=Et_pass.gettext (). toString (); //get to Sharedpreference objectSharedpreferences sp = getsharedpreferences ("info", mode_private); //Get editorEditor ed =Sp.edit (); Ed.putstring ("Name", name); Ed.putstring ("Pass", pass);    Ed.commit (); }}

In fact, many of the settings in Android are used Sharedpreference

For example

View Com.android.settings_preferences.xml files under Com.android.settings/shared_prefs

<?XML version= ' 1.0 ' encoding= ' utf-8 ' standalone= ' yes '?><Map>    <Booleanname= "Sound_effects"value= "true" />    <Booleanname= "com.android.inputmethod.latin/." Latinime "value= "true" />    <Booleanname= "FORCE_HW_UI"value= "false" />    <Booleanname= "jp.co.omronsoft.openwnn/." OPENWNNJAJP "value= "false" />    <Booleanname= "Auto_time"value= "false" />    <Booleanname= "Show_hw_screen_udpates"value= "false" />    <Booleanname= "Hour"value= "true" />    <Booleanname= "Dock_sounds"value= "false" />    <stringname= "Font_size">1.30</string>    <Booleanname= "Allow_mock_location"value= "false" />    <Booleanname= "keep_screen_on"value= "false" />    <Booleanname= "Show_hw_layers_udpates"value= "false" />    <stringname= "Date_format"></string>    <Booleanname= "ENABLE_ADB"value= "false" />    <Booleanname= "Enforce_read_external"value= "false" />    <Booleanname= "Show_touches"value= "false" />    <Booleanname= "Dtmf_tone"value= "true" />    <Booleanname= "Strict_mode"value= "false" />    <Booleanname= "com.example.android.softkeyboard/." SoftKeyboard "value= "false" />    <Booleanname= "Show_hw_overdraw"value= "false" />    <Booleanname= "Auto_zone"value= "false" />    <stringname= "Hdcp_checking">Drm-only</string>    <Booleanname= "Show_cpu_usage"value= "false" />    <Booleanname= "Debug_layout"value= "false" />    <Booleanname= "com.android.inputmethod.pinyin/." Pinyinime "value= "false" />    <Booleanname= "Pointer_location"value= "false" />    <Booleanname= "Disable_overlays"value= "false" />    <Booleanname= "Bugreport_in_power"value= "false" />    <Booleanname= "Haptic_feedback"value= "true" />    <Booleanname= "Immediately_destroy_activities"value= "false" />    <Booleanname= "Show_all_anrs"value= "false" />    <Booleanname= "Verify_apps_over_usb"value= "false" />    <Booleanname= "Lock_sounds"value= "true" />    <Booleanname= "Show_screen_updates"value= "false" />    <Booleanname= "FORCE_MSAA"value= "false" />    <Booleanname= "Wait_for_debugger"value= "false" /></Map>

Use of Android sharedpreference

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.