今天我们学习了使用SharedPreferences进行数据存储,首先让我们先来了解一下什么是SharedPreferences存储
1.SharedPreferences就是存储类,是Android平台上一个轻量级的存储类,用来存储少量数据;
2.以key-value(键值对)形式存储数据,可以存储的类型为:String,int,float,long,boolean;
3.存储位置在data/data/<包名>/shared_prefs目录下;
4.保存的数据以xml形式存储
而使用SharedPreferences写入数据的步骤是:
1.获得SharedPreferences对象;
2.获得Editor对象,通过SharedPreferences.Edit方法获得,Editor对象代表数据编辑对象,通过此对象可写入数据
3.通过Editor对象的putxxx函数,设置写入数据;
4.通过Editor对象的commit提交写入,代表提交本次写入的数据,如果不调用commit方法,数据不会写入到文件中
这里提供一个简单的SharedPreferences存储数据案例(保存账号密码)(附代码)
activity_shared_demo.xml
<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"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="100dp"
android:text="用户名"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="100dp"
android:hint="在这里输入你的用户名" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:text="密码"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:hint="在这里输入你的密码"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:checked="false"
android:text="记住密码" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_weight="1"
android:checked="false"
android:text="自动登录" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:text="登陆" />
</LinearLayout>
</LinearLayout>
贴上效果图
SharedDemoActivity.java
public class SharedDemoActivity extends AppCompatActivity {
private Button login;
private CheckBox checkBox, checkBox2;
private EditText editName, editPassword;
private int rememberFlag = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_demo);
bindID();
//从info.xml文件里取出节点对应的值
SharedPreferences sharedPreferences = getSharedPreferences("info.xml",MODE_PRIVATE);
if (sharedPreferences!=null){ //文件存在
String name = sharedPreferences.getString("name","");
String password = sharedPreferences.getString("password","");
int rememberFlag = sharedPreferences.getInt("rememberFlag",0);
//赋值给editName,因为账号默认记住账号
editName.setText(name);
if(rememberFlag == 1){//勾中复选框
checkBox.setChecked(true);
editPassword.setText(password);
}else {//没有勾中复选框
checkBox.setChecked(false);
}
}
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = editName.getText().toString();
String password = editPassword.getText().toString();
//1.创建SharedPreferences对象
SharedPreferences preferences = getSharedPreferences("info.xml",MODE_PRIVATE);
//2.创建Editor对象,写入值
SharedPreferences.Editor editor = preferences.edit();
//账号写入
editor.putString("name",username);
if (checkBox.isChecked()){
rememberFlag = 1;//勾中复选框
//账号写入
editor.putString("password",password);
editor.putInt("rememberFlag",rememberFlag);
}else {
rememberFlag = 0;//没有勾中复选框
editor.putInt("rememberFlag",rememberFlag);
}
//3.提交
editor.commit();
Toast.makeText(SharedDemoActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
}
});
}
private void bindID() {
login =findViewById(R.id.login);
editName =findViewById(R.id.editName);
editPassword =findViewById(R.id.editPassword);
checkBox =findViewById(R.id.checkBox);
checkBox2 =findViewById(R.id.checkBox2);
}
}
首先写login点击事件内的内容
获取用户输入的信息
String username = editName.getText().toString();
String password = editPassword.getText().toString();
下面按照步骤进行SharedPreferences存储
//1.创建SharedPreferences对象
SharedPreferences preferences = getSharedPreferences("info.xml",MODE_PRIVATE);
//2.创建Editor对象,写入值
SharedPreferences.Editor editor = preferences.edit();
//账号写入
editor.putString("name",username);
//3.提交
editor.commit();
到这里就完成了默认记住账号,下面设置记住密码的复选框勾选
在点击事件内判断复选框是否被勾选,进行想对应的设置
if (checkBox.isChecked()){
rememberFlag = 1;//勾中复选框
//账号写入
editor.putString("password",password);
editor.putInt("rememberFlag",rememberFlag);
}else {
rememberFlag = 0;//没有勾中复选框
editor.putInt("rememberFlag",rememberFlag);
}
然后在执行点击事件之前,还需要从info.xml文件里取出节点对应的值来对文件进行判断是否存在,还有判断复选框是否被勾选,这里定义一个int类型数值来表示复选框的勾选状态
SharedPreferences sharedPreferences = getSharedPreferences("info.xml",MODE_PRIVATE);
if (sharedPreferences!=null){ //文件存在
String name = sharedPreferences.getString("name","");
String password = sharedPreferences.getString("password","");
int rememberFlag = sharedPreferences.getInt("rememberFlag",0);
//赋值给editName,因为账号默认记住账号
editName.setText(name);
if(rememberFlag == 1){//勾中复选框
checkBox.setChecked(true);
editPassword.setText(password);
}else {//没有勾中复选框
checkBox.setChecked(false);
}
}