Use SharedPreferences in Android development to implement option box memory for QQ login (source code sharing)

Source: Internet
Author: User

This series of articles was produced by @ Lin Yu. Please indicate the source for reprinting.

Based on the simple implementation of SharedPreferences described in the previous blog, We can automatically display the user name and check whether to remember the user name and hide the login function when logging on to QQ, and display the practicality of SharedPreferences through instances.

The related code is as follows:

Package com. example. f15_sharedpreferences01; import java. util. hashMap; import java. util. map; import android. OS. bundle; import android. app. activity; import android. content. context; import android. content. sharedPreferences; import android. view. view; import android. widget. button; import android. widget. checkBox; import android. widget. editText; public class MainActivity extends Activity {private Button button; private CheckBox checkBox, checkBox2; private EditText editText; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) this. findViewById (R. id. button1); checkBox = (CheckBox) this. findViewById (R. id. checkBox1); checkBox2 = (CheckBox) this. findViewById (R. id. checkBox2); editText = (EditText) this. findViewById (R. id. editText1); Map
 
  
Map = getMsg ("login"); if (map! = Null &&! Map. isEmpty () {if (map. get ("username"). toString ()! = Null &&! Map. get ("username "). toString (). equals ("") {editText. setText (map. get ("username "). toString ();} checkBox. setChecked (Boolean) map. get ("isname"); checkBox2.setChecked (Boolean) map. get ("ispwd");} button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubHashMap
  
   
Map = new HashMap
   
    
(); If (editText. getText (). toString (). trim (). equals ("admin") {if (checkBox. isChecked () {map. put ("username", editText. getText (). toString (). trim ();} else {map. put ("username", "");} map. put ("isname", checkBox. isChecked (); map. put ("ispwd", checkBox2.isChecked (); saveMsg ("login", map) ;}}) ;}// Write public boolean saveMsg (String fileName, Map
    
     
Map) {boolean flag = false; // The general Mode uses private. It is safer to use SharedPreferences preferences = getSharedPreferences (fileName, Context. MODE_PRIVATE); SharedPreferences. editor editor = preferences. edit (); // The Map class provides a method called entrySet (), which returns a Map. object set after Entry instantiation. // Next, Map. the Entry class provides a getKey () method and a getValue () method. // Therefore, the above Code can be organized to better conform to the logic for (Map. entry
     
      
Entry: map. entrySet () {String key = entry. getKey (); Object object = entry. getValue (); // Add if (object instanceof Boolean) {Boolean new_name = (Boolean) object; editor. putBoolean (key, new_name);} else if (object instanceof Integer) {Integer integer = (Integer) object; editor. putInt (key, integer);} else if (object instanceof Float) {Float f = (Float) object; editor. putFloat (key, f);} else if (object instanceof Long) {Long l = (Long) object; editor. putLong (key, l);} else if (object instanceof String) {String s = (String) object; editor. putString (key, s) ;}} flag = editor. commit (); return flag;} // read data public Map
      
        GetMsg (String fileName) {Map
       
         Map = null; // The editSharedPreferences preferences = getSharedPreferences (fileName, Context. MODE_PRIVATE); map = preferences. getAll (); return map;} is not used for data reading ;}}
       
      
     
    
   
  
 


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.