import java.util.HashMap;
import java.util.Map;
import com.tz.utils.SpUtils;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_userName;
private EditText et_userPwd;
private CheckBox cb_uStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_userName = (EditText)findViewById(R.id.et_userName);
et_userPwd = (EditText)findViewById(R.id.et_userPwd);
cb_uStatus = (CheckBox)findViewById(R.id.cb_uStatus);
//如果我下次再来启动我们的QQ:把保存在sp中的用户名
//和密码读出来填写到对应的编辑框中
//sharedPreferences对应的名字不存在返回不是null
SharedPreferences sp = getSharedPreferences("QQ_user", Context.MODE_PRIVATE);
String name = null;
if((name = sp.getString("users",null))!=null){
et_userName.setText(name);
cb_uStatus.setChecked(true);
}
//sp.getLong(key, defValue)
}
public void login(View v){
//1,判断用户名和密码是够都正确
String uName = et_userName.getText().toString().trim();
String pwd = et_userPwd.getText().toString().trim();
if("tony".equals(uName)&&
"456".equals(pwd)){
Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG).show();
if(cb_uStatus.isChecked()){
//保存我们的用户名???---sharedPreferences中
//sp---xml文件
/*SharedPreferences sp = getSharedPreferences("QQ_user",Context.MODE_PRIVATE);
Editor editor = sp.edit();//editor本质就是一个集合
editor.putString("users", uName);
editor.putString("pwd", pwd);
editor.commit();//批处理
*/
Map<String,Object> map = new HashMap<String, Object>();
map.put("username",uName);
map.put("password", pwd);
//保存数据
SpUtils.sateData(this, "QQ_user",map);
//读取数据
String username= (String)SpUtils.getData(this, "QQ_user", "username", String.class);
String password = (String)SpUtils.getData(this, "QQ_user", "password", String.class);
Toast.makeText(this, "name="+username+",pwd="+password, Toast.LENGTH_LONG).show();
}
}else{
Toast.makeText(this, "登陆失败", Toast.LENGTH_LONG).show();
}
}
}
工具类
<pre name="code" class="html">
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
public class SpUtils {
// 保存数据
public static void sateData(Context context, String fileName,
Map<String, Object> map) {
SharedPreferences sp = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
Editor editor = sp.edit();
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// 判断类型
if (value instanceof String) {
editor.putString(key, (String) value);
} else if (value instanceof Boolean) {
editor.putBoolean(key, (Boolean) value);
} else if (value instanceof Float) {
editor.putFloat(key, (Float) value);
} else if (value instanceof Integer) {
editor.putInt(key, (Integer) value);
} else if (value instanceof Long) {
editor.putLong(key, (Long) value);
}
}
editor.commit();
}
// 读数据
public static Object getData(Context context, String fileName, String key,
Class clazz) {
Object obj = null;
SharedPreferences sp = context.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
Log.i("tz",String.class.getName());
if (clazz.getName().equals(String.class.getName())) {
obj = sp.getString(key, null);
} else if (clazz.getName().equals(Integer.class.getName())) {
obj = sp.getInt(key, 0);
} else if (clazz.getName().equals(Boolean.class.getName())) {
obj = sp.getBoolean(key, false);
} else if (clazz.getName().equals(Long.class.getName())) {
obj = sp.getLong(key, 0);
} else if (clazz.getName().equals(Float.class.getName())) {
obj = sp.getFloat(key, 0);
}
return obj;
}
}
sharedPrefereces:共享首选项
1,作用: 保存我们应用的一些配置信息(用户名 自动登录,夜光模式,偏好设置等)
2,其存储的位置:/data/data/<包名>/shared_prefs目录下
3,怎么样使用sp
1)根据Context获取sharedPreferences
2)利用edit方法获取Editor对象
3)通过Editor对象存储key-value键值对保存数据
4)通过commit方法提交数据
总结:
sp只能存储的数据类型是有限的(String int float long boolean Set)
相对于数据库而言,免去了创建表,创建数据库,写sql语句。
sp只是对数据存储的一种补充。
sp实际上就是一个xml文件。