1.SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。
2.实现SharedPreferences存储的步骤如下
(1)获得SharedPreferences对象
(2)获得SharedPreferences.Editor对象
(3)通过Editor接口的putXxx方法保存key-value对其中Xxx表示不同的数据类型
(4)通过Editor接口的commit方法保存key-value对
四种存储方式:
1.SharedPreferences
1)一种轻型的数据存储方式
2)本质:基于XML文件存储key-value键值对数据
3)常用来存储一些简单的配置信息
2.SQLite
3.Content Provider
4.File
ps:Android默认的XML解析器为DOM
创建对象默认和自定义
默认:PreferebceManager.getDefauleSharedPreferences(上下文);
自定义:getSharedPreferences(”自定义名称”,权限)
Editor editor = pref.edit();//编辑器对象
editor.commit();//提交,
editor.remove(key)//移除
ref.getString(key)//取出
//一、各种申明:
EditText etUserName,etUserPass;
CheckBox chk;
SharedPreferences pref;
Editor edtior;
etUserName = (EditText) findViewById(R.id.etuserName);
etUserPass = (EditText) findViewById(R.id.etuserpass);
chk = (CheckBox) findViewById(R.id.chkSaveName);
//二:为登录、取消按钮添加属性:(添加点击事件的另一个方法)
android:onClick="doClick"
public void doClick(View v){
switch (v.getId()) {
case R.id.btnLogin:
//去掉收尾的空格
String name = etUserName.getText().toString().trim();
String pass = etUserPass.getText().toString().trim();
if ("admin".equals(name)&&"123456".equals(pass)) {
if (chk.isChecked()) {
edtior.putString("userName", name);
edtior.commit();
}else {
edtior.remove("userName");
edtior.commit();
}
Toast.makeText(MainActivity.this, "登陆成功",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this, "禁止登陆", Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
//DemoSharedPreferences I
//第一种方法
//SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(this);
//第二种方法
/**
* 第一个参数:文件名.xml
* 第二个参数:模式;如MODE_PRIVATE:只有这个APP可以调用此对象sp
*/
SharedPreferences sp=getSharedPreferences("myPref", MODE_PRIVATE);
Editor editor=sp.edit();
//存储数据
editor.putString("Name", "慕课网");
editor.putInt("Age", 30);
editor.putLong("Time", System.currentTimeMillis());
editor.putBoolean("Default", true);
editor.commit();//如果这个不写的话,所有操作都无效
editor.remove("Default");
editor.commit();
//读取数据
//第二个参数:取不到数据的默认值
Log.i("TAG", sp.getString("Name", "默认值"));
Log.i("TAG", String.valueOf(sp.getInt("Age", 0)));
Log.i("TAG", String.valueOf(sp.getInt("Age1", 0)));//假设取不到
//Ps:Eclipse中,Error提示,双击错误地方即可锁定错误的位置。
源代码:
存取用户名eg. main.java
package com.example.sharedpreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText edName, edPass;
CheckBox checkBox;
SharedPreferences pref;
Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
// 获取权限
// PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);
// // 创建编辑器
// Editor editor = pref.edit();
// editor.putString("name", "zyh");
// editor.putInt("age", 20);
// editor.putLong("time", System.currentTimeMillis());
// editor.putBoolean("default", true);
// editor.commit();
// editor.remove("default");
// editor.commit();
//
// //前面是key 后面是没取到的默认值
// System.out.println(pref.getString("name", ""));
// System.out.println(pref.getInt("age", 0));
// //以此类推其他的键对值
edName = (EditText) findViewById(R.id.edName);
edPass = (EditText) findViewById(R.id.edPass);
checkBox = (CheckBox) findViewById(R.id.checkBox);
// pref = getSharedPreferences("mypref", MODE_PRIVATE);
editor = pref.edit();
String name = pref.getString("userName", "");
if (name == null) {
checkBox.setChecked(false);
} else {
checkBox.setChecked(false);
edName.setText(name);
}
}
public void doClick(View v) {
switch (v.getId()) {
case R.id.btnLogin:
// trim()是用来消除空格的
String name = edName.getText().toString().trim();
String pass = edPass.getText().toString().trim();
if ("admin".equals(name) && "123456".equals(pass)) {
if (checkBox.isChecked()) {
editor.putString("userName", name);
editor.commit();
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG)
.show();
} else {
editor.remove("userName");
editor.commit();
}
} else {
Toast.makeText(MainActivity.this, "用户名或者密码错误,禁止登陆", Toast.LENGTH_LONG)
.show();
}
break;
default:
break;
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名" />
<EditText
android:id="@+id/edName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/userPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码" />
<EditText
android:id="@+id/edPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
</LinearLayout>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存用户名" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:onClick="doClick"
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录" />
<Button
android:id="@+id/btnCancel"
android:onClick="doClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消" />
</LinearLayout>
</LinearLayout>