[Android] data, android data
Reprint Please Note: Reproduced in http://www.cnblogs.com/Liuyt-61/p/6637515.html
---------------------------------------------------------------
Four storage methods for Android data:
1. SharedPreferences
2. SQLite
3. Content Provider
4. File
---------------------- Split line ----------------------------------
1. SharedPreferences:
1. It is a lightweight data storage method.
2. The essence is to store key-value pairs based on XML files.
3. It is usually used to store some simple configuration information, such as the user name and password (the instance code is attached later)
1> the SharedPreferences object can only obtain data, but cannot be stored or modified.
Editor implements storage and Modification
2> steps for implementing storage:
① Use getSharedPreferences of the Activity class to obtain its object. The name of the file storing the key-value is specified by the first parameter of the getSharedPreferences method.
② Use the Edit interface of the SharedPreferences interface to obtain the SharedPreferences. Editor object.
③ Use the put ××× method of the SharedPreferences. Editor interface to save the key-value Pair
④ Use the commit () method of the SharedPreferences. Editor interface to save the key-value Pair and submit it.
Directly upload the instance code (Save the user name on the logon Interface)
Main. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content"> <TextView android: id = "@ + id/TV _username" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "username"/> <EditText android: id = "@ + id/et_username" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: EMS = "10" android: hint = "input username" android: inputType = "textPersonName"> </EditText> </LinearLayout> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content"> <TextView android: id = "@ + id/TV _password" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "password"/> <EditText android: id = "@ + id/et_password" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1" android: EMS = "10" android: hint = "input password" android: inputType = "textPassword"/> </LinearLayout> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content"> <CheckBox android: id = "@ + id/cb_remember" android: checked = "false" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Remember User Name"/> </LinearLayout> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: id = "@ + id/btn_login" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "doClick" android: text = "login"/> <Button android: id = "@ + id/btn_canel" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: onClick = "doClick" android: text = "cancel"/> </LinearLayout>
MainActivity. java
Package com. liuyt. s03_e28_sharedpreferences; import android. app. activity; import android. content. sharedPreferences; import android. content. sharedPreferences. editor; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. checkBox; import android. widget. editText; import android. widget. toast; public class MainActivity extends Activity {private Button btn_login, btn_canel; private EditText et_username, et_password; private CheckBox cb_remenber; private Editor editor; @ Override protected void onCreate (Bundle plugin) super. onCreate (savedInstanceState); setContentView (R. layout. main); // SharedPreferences pref = getSharedPreferences ("myPref", MODE_PRIVATE); // Editor editor = pref. edit (); // editor. putString ("name", "Liuyt"); // editor. putInt ("age", 18); // editor. commit (); // System. out. println (pref. getString ("name", ""); // System. out. println (pref. getInt ("age", 0); btn_canel = (Button) findViewById (R. id. btn_canel); btn_login = (Button) findViewById (R. id. btn_login); et_username = (EditText) findViewById (R. id. et_username); et_password = (EditText) findViewById (R. id. et_password); cb_remenber = (CheckBox) findViewById (R. id. cb_remember); SharedPreferences pref = getSharedPreferences ("myPref1", MODE_PRIVATE); editor = pref. edit (); String name = pref. getString ("username", ""); if (name = null) {cb_remenber.setChecked (false);} else {cb_remenber.setChecked (true); et_username.setText (name );}} public void doClick (View view) {switch (view. getId () {case R. id. btn_login: String name = et_username.getText (). toString (). trim (); // remove the trailing space String password = et_password.getText (). toString (). trim (); if ("admin ". equals (name) & "123456 ". equals (password) {if (cb_remenber.isChecked () {editor. putString ("username", name); editor. commit ();} else {editor. remove ("username"); editor. commit ();} Toast. makeText (MainActivity. this, "Logon successful", Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (MainActivity. this, "Login denied", Toast. LENGTH_SHORT ). show () ;}break; default: break ;}}}