SharedPreferences是以键值对来存储应用程序的配置信息的一种方式,以下以记住用户登录密码为例,来说明SharedPreferences的简单应用。
代码如下:
2
3
4 import android.app.Activity;
7 import android.content.Intent;
8 import android.content.SharedPreferences;
9 import android.content.SharedPreferences.Editor;
10 import android.os.Bundle;
15 import android.widget.CheckBox;
16 import android.widget.EditText;
17
24
25 /**
26 * 登录界面
27 */
28 public class LoginActivity extends Activity {
29
30 private static final String PREFS_NAME = " MyUserInfo " ;
34 private CheckBox chkSaveInfo;
35 private EditText txtUserName;
36 private EditText txtPassword;
73
74 /** Called when the activity is first created. */
75 @Override
76 public void onCreate(Bundle savedInstanceState) {
77 super .onCreate(savedInstanceState);
78 setContentView(R.layout.loginview);
83 txtUserName = (EditText) this .findViewById(R.id.txtUserName);
84 txtPassword = (EditText) this .findViewById(R.id.txtPassword);
88 LoadUserData();
89 }
90
91 /**
92 * 载入已记住用户信息
93 */
94 private void LoadUserData(){
95 SharedPreferences sp = getSharedPreferences(PREFS_NAME, 0 );
97 String userName = sp.getString( " userName " , "" );
98 String userPassword = sp.getString( " userPassword " , "" );
99 if ( ! ( "" .equals(userName) && "" .equals(userPassword))){
100 txtUserName.setText(userName);
101 txtPassword.setText(userPassword);
102 chkSaveInfo.setChecked( true );
103 }
104 }
105 }
107 /**
108 * 保存用户信息
109 */
110 private void SaveUserData(){
111 SharedPreferences sp = getSharedPreferences(PREFS_NAME, 0 );
112 Editor spEd = sp.edit();
113 if (chkSaveInfo.isChecked()){
114 spEd.putBoolean( " isSave " , true );
115 spEd.putString( " userName " , txtUserName.getText().toString());
116 spEd.putString( " userPassword " , txtPassword.getText().toString());
117 }
118 else {
119 spEd.putBoolean( " isSave " , false );
120 spEd.putString( " userName " , "" );
121 spEd.putString( " userPassword " , "" );
122 }
123 spEd.commit();
124 }
209 }
代码是从项目里摘出来的,只用于说明问题,请见谅。欢迎指正,不胜感激。
Preference是一种轻量级的键值(key-value)储存方式,可以用它来持久化存储一些变量的值,这些变量必须是基本数据类型。Preference存储的数据以XML文件形式保存,存储在/data/data/<包名>/shared_prefs目录下。
package com.android.test;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.view.*;
import android.view.View.OnClickListener;
public class MainActivityTest extends Activity {
private int flag=0;//1:自动登录
private SharedPreferences setting;
/** Called when the activity is first created. */
// 用户名输入框
private EditText et_username;
// 密码输入框
private EditText et_passwd;
// 登录按钮
private Button btn_login;
// 取消按钮
Button btn_reset;
//自动登录
CheckBox auto_log;
BtnListener btnListener=new BtnListener();
public void onCreate(Bundle savedInstanceState) {
///AlertDialog dialog=new AlertDialog.Builder(new MainActivityTest()).setTitle("dfsfs").create();
dialog.show();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 从当前事件中找到输入框,初始化
et_username = (EditText) findViewById(R.id.et_username);
et_passwd = (EditText) findViewById(R.id.et_passwd);
// 从当前事件中找到登录按钮,初始化
btn_login = (Button) findViewById(R.id.btn_login);
// btn_login.setOnClickListener(btnListener);
btn_login.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
if(et_username.getText().toString()==null||et_username.getText().toString().length()<=0)
{///et_username.getText().toString()==null要首先进行判断。==“”或者length<=0
et_username.setText("用户名不能为空");
}
// if(et_passwd.getText().toString()==null||et_passwd.getText().toString().length()<=0)
// {
// // et_username.setText("密码不能为空");
/// }
else
{
if(et_username.getText().toString().equals("sjk")&&et_passwd.getText().toString().equals("123"))
{ SharedPreferences.Editor editor=setting.edit();
if(auto_log.isChecked())
{
editor.putInt("auto_log", 1);
editor.putString("name", et_username.getText().toString());
editor.putString("password", et_passwd.getText().toString());
editor.commit();
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://t.sina.com.cn"));
startActivity(myIntent);/调用浏览器打开网页。
}
else
{
editor.clear().commit();
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sdu.edu.cn"));
startActivity(myIntent);/调用浏览器打开网页。
}
}
}
}
});
// ...
btn_reset = (Button) findViewById(R.id.btn_reset);
btn_reset.setOnClickListener(btnListener);
auto_log=(CheckBox) findViewById(R.id.auto_log);///自动登录复选框
setting=this.getSharedPreferences("auto_log",2);
if(setting.getInt("auto_log",0)==1)
{
String name_store=setting.getString("name","");
String passwd_store=setting.getString("password", "");
et_username.setText(name_store);
et_passwd.setText(passwd_store);
}
}
// 点击事件
private class BtnListener implements View.OnClickListener {
public void onClick(View v) {
// TODO Auto-generated method stub
/* if (v.getId() == R.id.btn_login) {
if(et_username.getText().toString()==null||et_username.getText().toString().length()<=0)
{///et_username.getText().toString()==null要首先进行判断。==“”或者length<=0
et_username.setText("用户名不能为空");
}
if(et_passwd.getText().toString()==null||et_passwd.getText().toString().length()<=0)
{
et_username.setText("密码不能为空");
}
}*/
if (v.getId() == R.id.btn_reset) {
finish();
}
}
}
}