1.SharedPreFerences是什么
是安卓的一种最轻量的储存类,储存为xml文件储存到/data/data/包名/shared_prefs下,一般用来存储一些比较简单的数据,比如用户名姓名,密码等等
2.如何储存数据
SharedPreFerences 储存数据分为四步
1.新建sharedPreFerences对象
2.通过sharedPreFerences.editor()创建editor对象
3.储存数据,通过editor.putString()、editor.putInt()方法储存,括号内是以键值对的形式存在例如editor.putString(“name”,”小明”)
4.提交数据 editor.commit();
附上一段简单的代码
//sp为新建xml文件的文件名,MODE模式,PRIVATE私有
SharedPreferences sharedPreferences=getSharedPreferences("sp",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("name","张三");
editor.commit();
3.如何读取数据
editor提交了数据后,如果我们要访问数据,就可以通过sharedPreferences对象读取,editor
是put,sharedPreferences就是get,后面还要加上一个默认值
SharedPreferences sharedPreferences=getSharedPreferences("sp",MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("name","张三");
editor.commit();
sp_tv.setText(sharedPreferences.getString("name",""));
4.记住密码案例
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#e62828"
android:orientation="horizontal">
<ImageButton
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="#e62828"
android:src="@mipmap/back_btn" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="10dp"
android:text="登录"
android:textColor="#ffffff"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机:"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:id="@+id/login_name"
android:layout_width="250dp"
android:layout_height="match_parent"
android:hint="请输入你的手机号!" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:id="@+id/login_psd"
android:layout_width="250dp"
android:layout_height="match_parent"
android:hint="请输入你的密码!" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<CheckBox
android:id="@+id/login_cb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="记住密码"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/login_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#e62828"
android:text="登录"
android:textColor="#ffffff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:background="#e62828"
android:text="注册"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
package com.example.zuoye1;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private EditText login_name;
private EditText login_psd;
private Button login_btn;
private CheckBox login_cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
bindID();
SharedPreferences sharedPreferences = getSharedPreferences("name", MODE_PRIVATE);
int checked=sharedPreferences.getInt("isCheck",0);
//判断name文件中是否有数据,复选按钮是否被点击
if(checked==1){
login_name.setText(sharedPreferences.getString("name",""));
login_psd.setText(sharedPreferences.getString("psd",""));
//如果有数据,让复选按钮继续保持点击状态
login_cb.setChecked(true);
}else {
//如果没有数据,让复选按钮继续保持未点击状态
login_cb.setChecked(false);
}
}
private void bindID() {
login_name = findViewById(R.id.login_name);
login_psd = findViewById(R.id.login_psd);
login_btn = findViewById(R.id.login_btn);
login_btn.setOnClickListener(this);
login_cb=findViewById(R.id.login_cb);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login_btn:
SharedPreferences sharedPreferences=getSharedPreferences("name", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
//判断是否点击了复选按钮
if (login_cb.isChecked()) {
//复选按钮被点击时将密码和用户名存放进name.xml文件中,
并且设置一个标志位通知复选按钮按下了
String name = login_name.getText().toString();
String psd = login_psd.getText().toString();
editor.putString("name", name);
editor.putString("psd", psd);
editor.putInt("isCheck",1);
}else {
//复选按钮未被点击时将name.xml文件中密码和用户名清空,
并且设置一个标志位通知复选按钮未按
editor.putString("name", "");
editor.putString("psd", "");
editor.putInt("isCheck",0);
}
editor.commit();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivityForResult(intent, 1);
break;
default:
break;
}
}
}