Learn android from scratch (Data Storage (1) SharedPreferences attribute file. 35 .)
Store data on the web with your own network server. network Storage
Today, we will take a look at the Shared Preferences attribute file storage method to store simple data.
We can use Shared Preferences to store simple booleans, floats, ints, longs, and strings data and save it as an xml file as a key-value pair.
To instantiate Shared Preferences, we can use
GetSharedPreferences () andgetPreferences()
These two methods
The first method requires passing in a file name and storage mode.
The second method has only one property file by default. You only need to pass in a storage mode.
Storage Mode:
MODE_PRIVATE is only available for this application
MODE_APPEND: appendable
MODE_WORLD_READABLE
, Which can be read by other applications
MODE_WORLD_WRITEABLE
. Can be written by other applications
For more information, see code comments.
Xml file
JAVA files
Package com. example. sharedpreferences; import android. app. activity; import android. content. sharedPreferences; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity {private SharedPreferences sharedPreferences; private Button saveData, getDate; public static final String FILENAME = "flyou"; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); sharedPreferences = getSharedPreferences (FILENAME, MODE_PRIVATE); saveData = (Button) this. findViewById (R. id. button1); getDate = (Button) this. findViewById (R. id. button2); saveData. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubSharedPreferences. editor editor = sharedPreferences. edit (); editor. putString ("username", "jay"); editor. putString ("password", "553274238"); Boolean flag = editor. commit (); Toast. makeText (MainActivity. this, "execution completed, execution result: -->" + flag, 2 ). show () ;}}); getDate. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubString username = sharedPreferences. getString ("username", "no matching information found"); String password = sharedPreferences. getString ("password", "User password not found"); Toast. makeText (MainActivity. this, "username: -->" + username + ", password: -->" + password, 2 ). show ();}});}}
Next, use the change method to implement the function of remembering the account and password locally.
It may involve some unknown knowledge. You can first understand and review other components you have learned.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + eG1szsS8/jwvcD4KPHA + 1ve958PmPC9wPgo8cD48cHJlIGNsYXNzPQ = "brush: java;">
Logon Interface
JAVA files
Package com. example. sharepreferencesdemo; import android. app. activity; import android. app. progressDialog; import android. content. intent; import android. content. sharedPreferences; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. checkBox; import android. widget. editText; public class MainActivity extends Activity {private SharedPreferences sharedPreference S; private Button login; private CheckBox checkBox; private EditText username, password; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); login = (Button) this. findViewById (R. id. button1); checkBox = (CheckBox) this. findViewById (R. id. checkBox1); username = (EditText) this. findViewById (R. id. editText1); password = (EditTex T) this. findViewById (R. id. editText2); sharedPreferences = getPreferences (MODE_PRIVATE); // instantiate the sharedPreferences object String usernameString = sharedPreferences through getPreferences. getString ("username", ""); // read username. setText (usernameString); // set the content in the edit box // obtain the selected status of the check box. if so, remember the password if (sharedPreferences. getBoolean ("checked", false) {// obtain the password String passwordString = sharedPreferences. getString ("password", ""); // Set the password in the edit box. setText (passwordString);} login. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub // instantiate the Editor object by using the edit method to store information about SharedPreferences. editor editor = sharedPreferences. edit (); // store information in the form of key-value pairs editor. putString ("username", username. getText (). toString (); editor. putString ("password", password. getText (). toString (); // judge If (checkBox. isChecked () {editor. putBoolean ("checked", true);} else {editor. putBoolean ("checked", false);} // executes the storage operation editor. commit (); // set the progress dialog box final ProgressDialog dialog = new ProgressDialog (MainActivity. this); // set the title dialog. setTitle ("User Logon"); // sets the prompt message dialog. setMessage ("logging in, please wait ...... "); // Start progress dialog box dialog. onStart (); // delayed Thread Operation new Thread () {@ Overridepublic void run () {// TODO Auto-generated method stubtry {// sleep for 3 seconds Thread. sleep (3*1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {// the dialog box disappears. dismiss (); // sets Intent intent Intent = new Intent (MainActivity. this, main. class); // transfer intent information intent. putExtra ("username", username. getText (). toString (); // start activity to jump to startActivity (intent); MainActivity. this. finish ();}}}. start (); // start the thread operation // display the dialog box dialog. show ();}});}}
Login interface
Package com. example. sharepreferencesdemo; import android. app. activity; import android. content. intent; import android. graphics. color; import android. OS. bundle; import android. view. gravity; import android. widget. textView; public class main extends Activity {private TextView text; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); super. setContentView (R. layout. linearlayout); text = (TextView) this. findViewById (R. id. textView3); Intent intent = getIntent (); text. setTextSize (15); text. setGravity (Gravity. CENTER_HORIZONTAL); text. setTextColor (Color. CYAN); text. setText ("Welcome:" + intent. getStringExtra ("username "));}}
Do not click Remember password, the second login.
Click Remember password to log on
Click Remember password to log on for the third time
This article introduces the storage of SharedPreferences attribute files. We can quickly store and conveniently read small data.
Next prediction: Internal Storage