package com.sky;
import android.app.Activity;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.widget.EditText;
public class saveData extends Activity {
/** Called when the activity is first created. */
public static final String SETTING_INFOS = "SETTING_Infos";
public static final String NAME = "NAME";
public static final String PASSWORD = "PASSWORD";
private EditText field_name; //接收用户名的组件
private EditText filed_pass; //接收密码的组件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
field_name = (EditText) findViewById(R.id.EditText01); //首先获取用来输入用户名的组件
filed_pass = (EditText) findViewById(R.id.EditText02); //同时也需要获取输入密码
// Restore preferences
//获取一个SharedPreferences对象,SETTING_INFOS与SETTING_INFOS.XML文件相对应
//效果相当于读取SETTING_INFOS.XML文件,如果文件不存在,则进行创建
SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0);
//取出保存的NAME,取出改字段名的值,不存在则创建默认为空
String name = settings.getString(NAME, "");
String password = settings.getString(PASSWORD, ""); //取出保存的PASSWORD,同上
field_name.setText(name); //将取出来的用户名赋予field_name
filed_pass.setText(password); //将取出来的密码赋予filed_pass
setTitle(name+password);//将NAME和PASSWORD显示在标题栏
}
@Override
protected void onStop()
{
// TODO Auto-generated method stub
super.onStop();
//打开SETTING_INFOS.XML
//0表示打开模式,有则读,没有则创建
SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0);
//下面一句的语法比较神奇,每次执行都返回一个setting.edit(),commit()表示执行
settings.edit()
.putString(NAME, field_name.getText().toString())
.putString(PASSWORD, filed_pass.getText().toString())
.commit();//将field_name赋值给NAME字段,将filed_pass赋值给PASSWORD字段
}
}
//要查看录入的数据,进到DDMS->File Explorer /<package name>/shared_prefs/SETTING_INFOS.XML
import android.app.Activity;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.widget.EditText;
public class saveData extends Activity {
/** Called when the activity is first created. */
public static final String SETTING_INFOS = "SETTING_Infos";
public static final String NAME = "NAME";
public static final String PASSWORD = "PASSWORD";
private EditText field_name; //接收用户名的组件
private EditText filed_pass; //接收密码的组件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
field_name = (EditText) findViewById(R.id.EditText01); //首先获取用来输入用户名的组件
filed_pass = (EditText) findViewById(R.id.EditText02); //同时也需要获取输入密码
// Restore preferences
//获取一个SharedPreferences对象,SETTING_INFOS与SETTING_INFOS.XML文件相对应
//效果相当于读取SETTING_INFOS.XML文件,如果文件不存在,则进行创建
SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0);
//取出保存的NAME,取出改字段名的值,不存在则创建默认为空
String name = settings.getString(NAME, "");
String password = settings.getString(PASSWORD, ""); //取出保存的PASSWORD,同上
field_name.setText(name); //将取出来的用户名赋予field_name
filed_pass.setText(password); //将取出来的密码赋予filed_pass
setTitle(name+password);//将NAME和PASSWORD显示在标题栏
}
@Override
protected void onStop()
{
// TODO Auto-generated method stub
super.onStop();
//打开SETTING_INFOS.XML
//0表示打开模式,有则读,没有则创建
SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0);
//下面一句的语法比较神奇,每次执行都返回一个setting.edit(),commit()表示执行
settings.edit()
.putString(NAME, field_name.getText().toString())
.putString(PASSWORD, filed_pass.getText().toString())
.commit();//将field_name赋值给NAME字段,将filed_pass赋值给PASSWORD字段
}
}
//要查看录入的数据,进到DDMS->File Explorer /<package name>/shared_prefs/SETTING_INFOS.XML