Android [intermediate tutorial] Chapter 1 Shared Preferences for data storage

Source: Internet
Author: User

 

After reading the Android [basic tutorial], I can finally learn the [intermediate tutorial]. Haha, this time we will learn about data storage in Android development. First, we will learn Shared Preferences, shared Preferences simply stores the Key-Value of the data. I believe anyone who has learned java knows that there is a type of Map, and the data is saved in the form of Key-Value. however, Shared Preferences is essentially different from Map. Map only exists in the program, while Shared Preferences stores data on hardware devices (OK, here the hardware device is the mobile phone ). well, let's not talk about it. We use Shared Preferences to store the number of Goku monsters. You don't want Goku to play monsters again every time it starts a program? Maybe Wukong is looking for you.

 

Main. xml

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: orientation = "vertical" android: layout_width = "match_parent"

Android: layout_height = "match_parent">

<TextView android: layout_width = "wrap_content" android: id = "@ + id/textView1"

Android: textAppearance = "? Android: attr/textAppearanceLarge"

Android: layout_height = "wrap_content" android: text = "TextView"> </TextView>

<EditText android: id = "@ + id/editText1" android: layout_width = "match_parent"

Android: layout_height = "wrap_content" android: inputType = "number">

<RequestFocus> </requestFocus>

</EditText>

<LinearLayout android: layout_width = "match_parent"

Android: id = "@ + id/linearLayout2" android: layout_height = "wrap_content">

<Button android: layout_width = "wrap_content"

Android: layout_height = "wrap_content" android: id = "@ + id/addOne"

Android: text = "Wukong killed another monster"> </Button>

<Button android: layout_width = "wrap_content"

Android: layout_height = "wrap_content" android: id = "@ + id/read_edit"

Android: text = "reading data in Edit"> </Button>

</LinearLayout>

<LinearLayout android: layout_width = "match_parent"

Android: id = "@ + id/linearLayout1" android: layout_height = "wrap_content">

<Button android: layout_width = "wrap_content"

Android: layout_height = "wrap_content" android: id = "@ + id/save"

Android: text = "Data Storage"> </Button>

<Button android: layout_width = "wrap_content"

Android: layout_height = "wrap_content" android: id = "@ + id/read"

Android: text = "read data"> </Button>

<Button android: layout_width = "wrap_content"

Android: layout_height = "wrap_content" android: id = "@ + id/clear"

Android: text = "Clear Data"> </Button>

</LinearLayout>

 

</LinearLayout>

 

Here, only an EditText and several buttons are added. Let's take a look at the java source code:

 

Import android. app. Activity;

Import android. content. SharedPreferences;

Import android. content. SharedPreferences. Editor;

Import android. OS. Bundle;

Import android. view. View;

Import android. view. View. OnClickListener;

Import android. widget. Button;

Import android. widget. EditText;

Import android. widget. TextView;

 

Public class Shared_PreferencesDemo extends Activity implements OnClickListener

{

Private int count = 0;

Private String str;

Private TextView text;

Private EditText edit_text;

Private Button add;

Private Button save;

Private Button read;

Private View clear;

Private Button read_edit;

Private SharedPreferences preferences;

Private Editor edit;

@ Override

Protected void onCreate (Bundle savedInstanceState)

{

// TODO Auto-generated method stub

Super. onCreate (savedInstanceState );

SetContentView (R. layout. shared_preferences );

// Obtain the SharedPreferences object that can only be read and written by this program.

Preferences = getSharedPreferences ("shared", 0 );

// Obtain the edit object of SharedPreferences

Edit = preferences. edit ();

Str = "Wukong killed" + count + "only monsters .";

Text = (TextView) findViewById (R. id. textView1 );

Text. setText (str );

Edit_text = (EditText) findViewById (R. id. editText1 );

Edit_text.setText (String. valueOf (count ));

Add = (Button) findViewById (R. id. addOne );

Add. setOnClickListener (this );

Read_edit = (Button) findViewById (R. id. read_edit );

Read_edit.setOnClickListener (this );

Save = (Button) findViewById (R. id. save );

Save. setOnClickListener (this );

Read = (Button) findViewById (R. id. read );

Read. setOnClickListener (this );

Clear = (Button) findViewById (R. id. clear );

Clear. setOnClickListener (this );

}

// Button event monitoring

@ Override

Public void onClick (View v)

{

Switch (v. getId ()){

// Wukong killed another monster button event

Case R. id. addOne:

// Increase the number of monsters by 1

Count ++;

// Refresh the value in the TextView and EditText controls.

Refresh ();

Break;

// Read the data button event in Edit

Case R. id. read_edit:

// Retrieve the value in SharedPreferences

Count = Integer. parseInt (edit_text.getText (). toString ());

Refresh ();

Break;

Case R. id. save:

// Save the number of monsters killed by Wukong to SharedPreferences

Edit. putString ("number", String. valueOf (count ));

Edit. commit ();

Break;

Case R. id. read:

// Retrieve the number of monsters killed by Wukong from SharedPreferences

Count = Integer. valueOf (preferences. getString ("number", "0 "));

Refresh ();

Break;

Case R. id. clear:

// Clear all data in SharedPreferences

Edit. clear ();

Edit. commit ();

Count = 0;

Refresh ();

Break;

}

}

// Custom refresh

Private void refresh ()

{

Str = "Wukong killed" + count + "only monsters .";

Text. setText (str );

Edit_text.setText (String. valueOf (count ));

}

}

Okay. Let's take a look.

 

The data storage location is data/your own package

From: kangkangz4 Column

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.