Using SharedPreferences in the program's data space to save data to generate xml document
package com.hu.data;
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;
public class ShDemoActivity extends Activity {
private EditText etName, etAge, etScore;
private Button btWrite, btRead;
private SharedPreferences sharedPrefrences;
private Editor editor;
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
etName = (EditText) findViewById (R.id.editTextName) ;/ / get control
etAge = (EditText) findViewById (R.id.editTextAge);
etScore = (EditText) findViewById (R.id.editTextScore);
btWrite = (Button) findViewById (R.id.buttonWrite);
btRead = (Button) findViewById (R.id.buttonRead);
sharedPrefrences = this.getSharedPreferences ("user", MODE_WORLD_READABLE) ;/ / get the SharedPreferences, will generate user.xml
editor = sharedPrefrences.edit ();
btWrite.setOnClickListener (new OnClickListener () {/ / Write button event
;
public void onClick (View arg0) {
; String name = etName.getText (). toString ();
int age = Integer.parseInt (etAge.getText (). toString ());
float score = Float.parseFloat (etScore.getText (). toString () ) ;/ / get user input data
editor.putString (" name ", name);
editor.putInt (" age " , age);
editor.putFloat ("score", score ) ;/ / write data to xml
editor.commit () ;/ / submit
}
;});
btRead.setOnClickListener ( new OnClickListener () {/ / read out the button event
; public void onClick (View v) {
; String name = sharedPrefrences.getString ("name", null);
int age = sharedPrefrences.getInt ("age", 0);
; float score = sharedPrefrences.getFloat ("score", 60.0f) ;/ / read the data
; etName.setText (name);
etAge.setText (Integer.toString (age));
etScore.setText (Float.toString (score)) ;/ / display data
}
});
}
}
layout file is:
android: layout_width = " ; fill_parent "
android: layout_height =" fill_parent "
android: orientation =" vertical ">
;
android: layout_height = "wrap_content">
; android: layout_width = "wrap_content"
; android: layout_height = "wrap_content"
android: text = "Name : "
android: textAppearance ="? android: attr / textAppearanceLarge "/>
android: layout_width = " wrap_content "
android: layout_height =" wrap_content "
android: layout_weight = "1">
;
android: layout_height = "wrap_content">
;
android: layout_width =" wrap_content "
android: layout_height = "wrap_content"
android: text = "Age:"
android: textAppearance = " ? android: attr / textAppearanceLarge "/>
; android: layout_width = "wrap_content"
android: layout_height = "wrap_content" ;
android: layout_weight = "1" />
android: layout_height = "wrap_content">
android: layout_width =" wrap_content "
; android: layout_height = "wrap_content"
; android: text = "Score:"
android: textAppearance = "? android: attr / textAppearanceLarge "/>
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: layout_weight = "1" />
;
android: layout_height = "wrap_content">
; android: id = "@ + id / buttonWrite "
android: layout_width =" wrap_content "
android: layout_height = "wrap_content"
android: text = "write" />
android: id = "@ + id / buttonRead"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "read-out" /> < br />
SharePreferences store data through the acquisition Editor Editor object to operate.
Insert Data:
call Editor.putxxxx method, two parameters are key and value.
Get Data:
call Editor.getxxxx method, two parameters are key and there is no default value for the specified key .
Delete data:
call Editor.remove methods, parameters for the specified key.
clear all data:
call Editor.clear method
method calls have to perform all of the above methods to submit Editor.commit.
没有评论:
发表评论