[Android development and learning 07] Preferences, a powerful tool for storing simple data

Source: Internet
Author: User

I. Basic Knowledge:

Preferences is a lightweight database storage mechanism used to record game scores and the last login time of applications.

 

 

 

Ii. Programming implementation:

1. Edit the interface (res \ layout \ main. xml ):


[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "25dip"
Android: id = "@ + id/TextView01"
/> <! -- Add TextView -->
</LinearLayout>

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "25dip"
Android: id = "@ + id/TextView01"
/> <! -- Add TextView -->
</LinearLayout>
2. Edit the code (\ src \ wyf \ zcl \ MyActivity. java ):


[Java]
Package wyf. zcl;
Import java. util. Date; // introduce related packages
Import android. app. Activity; // introduce related packages
Import android. content. Context; // introduce related packages
Import android. content. SharedPreferences; // introduce related packages
Import android. OS. Bundle; // introduce related packages
Import android. widget. TextView; // introduce related packages
Import android. widget. Toast; // introduce related packages
Public class MyActivity extends Activity {
/** Called when the activity is first created .*/
Private TextView TV;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
SharedPreferences sp = this. getSharedPreferences ("preset pre", Context. MODE_PRIVATE );
// Return a SharedPreferences instance. The first parameter is the Preferences name, and the second parameter is the default operation.
String lastLogin = sp. getString (// time when the last access was read from SharedPreferences
"Ll", // key value
Null // Default Value
);
If (lastLogin = null ){
LastLogin = "Welcome, this is your first visit to this Preferences ";
} Else {
LastLogin = "welcome back. You logged on to" + lastLogin + "last time ";
}
// Write back the access time to SharedPreferences
SharedPreferences. Editor editor = sp. edit ();
Editor. putString ("ll", new Date (). toLocaleString (); // Add the current time to the editor
Editor. commit (); // submit editor
TV = (TextView) this. findViewById (R. id. TextView01 );
TV. setText (lastLogin );
}
}

Package wyf. zcl;
Import java. util. Date; // introduce related packages
Import android. app. Activity; // introduce related packages
Import android. content. Context; // introduce related packages
Import android. content. SharedPreferences; // introduce related packages
Import android. OS. Bundle; // introduce related packages
Import android. widget. TextView; // introduce related packages
Import android. widget. Toast; // introduce related packages
Public class MyActivity extends Activity {
/** Called when the activity is first created .*/
Private TextView TV;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
SharedPreferences sp = this. getSharedPreferences ("preset pre", Context. MODE_PRIVATE );
// Return a SharedPreferences instance. The first parameter is the Preferences name, and the second parameter is the default operation.
String lastLogin = sp. getString (// time when the last access was read from SharedPreferences
"Ll", // key value
Null // Default Value
);
If (lastLogin = null ){
LastLogin = "Welcome, this is your first visit to this Preferences ";
} Else {
LastLogin = "welcome back. You logged on to" + lastLogin + "last time ";
}
// Write back the access time to SharedPreferences
SharedPreferences. Editor editor = sp. edit ();
Editor. putString ("ll", new Date (). toLocaleString (); // Add the current time to the editor
Editor. commit (); // submit editor
TV = (TextView) this. findViewById (R. id. TextView01 );
TV. setText (lastLogin );
}
}
3. Running effect:

 

 

 

 

 

 

 

 

 

 


 

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.