android 当中有集中数据存储方式,比如说sqlite,还有一个比较轻量级的,那就是SharedPreferences,相当于web中的cookie,这个存储的原理就是通过键值对,进行存取
如果第一次使用SharedPreferences,他会在/data/data/包命/shared_prefs/下生成xxx.xml,这个xxx.xml就是存储你的键值对,要实现记住密码功能,其实很简单:代码如下
public class SharedPreferencesForLogin{
SharedPreferences sp;
SharedPreferences.Editor editor;
Context context;
public SharedPreferencesForLogin(Context c,String name){
context = c;
sp = context.getSharedPreferences(name, 0);
editor = sp.edit();
}
//向SharedPreferences中注入数据
public void putValue(String key, String value){
editor = sp.edit();
editor.putString(key, value);
//这个提交很重要,别忘记,对xml修改一定别忘了commit()
editor.commit();
}
//根据Key获取对应的Value
public String getValue(String key){
return sp.getString(key, null);
}
//清除SharedPreferences中的数据,比如点击“忘记密码”
public void clear(){
editor = sp.edit();
editor.clear();
editor.commit();
}
}
那么在我们的Activity中就直接调用即可(记住密码)
public class SharedPreferencesForLogin{
SharedPreferences sp;
SharedPreferences.Editor editor;
Context context;
public SharedPreferencesForLogin(Context c,String name){
context = c;
sp = context.getSharedPreferences(name, 0);
editor = sp.edit();
}
//向SharedPreferences中注入数据
public void putValue(String key, String value){
editor = sp.edit();
editor.putString(key, value);
//这个提交很重要,别忘记,对xml修改一定别忘了commit()
editor.commit();
}
//根据Key获取对应的Value
public String getValue(String key){
return sp.getString(key, null);
}
//清除SharedPreferences中的数据,比如点击“忘记密码”
public void clear(){
editor = sp.edit();
editor.clear();
editor.commit();
}
}
那么在我们的Activity中就直接调用即可(记住密码)
/**
*
*记住用户名、密码
*/
String userNameValue = userName.getText().toString();
String passwordValue = password.getText().toString();
SharedPreferencesForLogin spfl = new SharedPreferencesForLogin(Login.this,"xml文件的名字");
spfl.putValue("USER_NAME",userNameValue);
spfl.putValue("PASSWORD",passwordValue);
/**
*
*取出用户名、密码
*/
SharedPreferencesForLogin spfl = new SharedPreferencesForLogin(Login.this,"xml文件的名字");
String userNameValue = spfl.getValue("USER_NAME");
String passwordValue = spfl.getValue("PASSWORD");
userName.setText(userNameValue);
passwrod.setText(passwordValue);
/**
*
*清除数据
*/
SharedPreferencesForLogin spfl = new SharedPreferencesForLogin(Login.this,"xml文件的名字");
spfl.clear();
*
*取出用户名、密码
*/
SharedPreferencesForLogin spfl = new SharedPreferencesForLogin(Login.this,"xml文件的名字");
String userNameValue = spfl.getValue("USER_NAME");
String passwordValue = spfl.getValue("PASSWORD");
userName.setText(userNameValue);
passwrod.setText(passwordValue);
/**
*
*清除数据
*/
SharedPreferencesForLogin spfl = new SharedPreferencesForLogin(Login.this,"xml文件的名字");
spfl.clear();
下面我有一个类似的项目,提供给你参考
[code]
public void onCreate(Bundle savedInstanceState) {
...
执行onCreate 方法初始化的时候就获得SharedPreferences中是否保存有用户数据
//userinfo表示获得对应的userinfo.xml中的数据
SharedPreferences sharedPreference = this.getSharedPreferences("userinfo", MODE_PRIVATE);
//获得保存在userinfo.xml文件中的数据
//参数1:获得保存在userinfo.xml文件中的key
//参数2:当key不存在或者没有对应的值的时候返回的默认值
String userName = sharedPreference.getString("userName", "");
String passw = sharedPreference.getString("password", "");
//设置到组件中
name.setText(userName);
password.setText(passw);
//自动登录的判断
}
//按钮的事件处理
View.OnClickListener click = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.submit:
if(isSave.isChecked()){//判断是否勾选自动登录选项
//获得组件中的数据(登录帐号和密码)
String userName = name.getText().toString();
String pass = password.getText().toString();
//通过Context获得SharedPreferences对象
//参数 1:将数据保存到什么文件中,文件名称 (如果指定的文件不存在就创建一个新的)
//参数 2: 对文件的操作模式
//MODE_PRIVATE 默认模式 私有模式(当前应用程序可以操作)
//MODE_WORLD_READABLE 和 MODE_WORLD_WRITEABLE 可以提供给其他的应用程序进行读或者写 模式
SharedPreferences sharedPreference = MainActivity.this.getSharedPreferences("userinfo", MODE_PRIVATE);
//获得一个Editor对象,进行数据保存
Editor myed = sharedPreference.edit();
//设置相应保存的数据,(一般保存一下应用程序的配置信息)
//使用和Map方式一样的
myed.putString("userName", userName);
myed.putString("password", pass);
//提交操作(将操作的数据进行保存到userinfo.xml中)
myed.commit();
Toast.makeText(MainActivity.this, "已经保存您的数据!", Toast.LENGTH_LONG).show();
}
[/code]
[code]
public void onCreate(Bundle savedInstanceState) {
...
执行onCreate 方法初始化的时候就获得SharedPreferences中是否保存有用户数据
//userinfo表示获得对应的userinfo.xml中的数据
SharedPreferences sharedPreference = this.getSharedPreferences("userinfo", MODE_PRIVATE);
//获得保存在userinfo.xml文件中的数据
//参数1:获得保存在userinfo.xml文件中的key
//参数2:当key不存在或者没有对应的值的时候返回的默认值
String userName = sharedPreference.getString("userName", "");
String passw = sharedPreference.getString("password", "");
//设置到组件中
name.setText(userName);
password.setText(passw);
//自动登录的判断
}
//按钮的事件处理
View.OnClickListener click = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.submit:
if(isSave.isChecked()){//判断是否勾选自动登录选项
//获得组件中的数据(登录帐号和密码)
String userName = name.getText().toString();
String pass = password.getText().toString();
//通过Context获得SharedPreferences对象
//参数 1:将数据保存到什么文件中,文件名称 (如果指定的文件不存在就创建一个新的)
//参数 2: 对文件的操作模式
//MODE_PRIVATE 默认模式 私有模式(当前应用程序可以操作)
//MODE_WORLD_READABLE 和 MODE_WORLD_WRITEABLE 可以提供给其他的应用程序进行读或者写 模式
SharedPreferences sharedPreference = MainActivity.this.getSharedPreferences("userinfo", MODE_PRIVATE);
//获得一个Editor对象,进行数据保存
Editor myed = sharedPreference.edit();
//设置相应保存的数据,(一般保存一下应用程序的配置信息)
//使用和Map方式一样的
myed.putString("userName", userName);
myed.putString("password", pass);
//提交操作(将操作的数据进行保存到userinfo.xml中)
myed.commit();
Toast.makeText(MainActivity.this, "已经保存您的数据!", Toast.LENGTH_LONG).show();
}
[/code]