1.SharedPreferences是什么
SharedPreferences是一种轻量级的数据存储方式,用来保存应用的一些常用配置,它是Android数据持久化方法中最简单的一种。
2.SharedPreferences如何存储数据,如何读取数据
1、根据Context获取SharedPreferences对象
2、利用edit()方法获取Editor对象。
3、通过Editor对象存储key-value键值对数据。
4、通过commit()方法提交数据。
使用SharedPreferences储存存数据方法如下:
//实例化SharedPreferences对象(第一步)
SharedPreferences mySharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE);
//实例化SharedPreferences.Editor对象(第二步)
SharedPreferences.Editor editor = mySharedPreferences.edit();
//用putString的方法保存数据
editor.putString("name", "Karl");
editor.putString("habit", "sleep");
//提交当前数据
editor.commit();
//使用toast信息提示框提示成功写入数据
Toast.makeText(this, "数据成功写入SharedPreferences!" , Toast.LENGTH_LONG).show();
使用SharedPreferences读取数据方法如下:
/同样,在读取SharedPreferences数据前要实例化出一个SharedPreferences对象
SharedPreferencessharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE);
// 使用getString方法获得value,注意第2个参数是value的默认值
String name =sharedPreferences.getString("name", "");
String habit =sharedPreferences.getString("habit", "");
//使用toast信息提示框显示信息
Toast.makeText(this, "读取数据如下:"+"\n"+"name:" + name + "\n" + "habit:" + habit,Toast.LENGTH_LONG).show();
3.记住密码案例
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#f4efef"
tools:context="com.example.dfcn.paopao.LoginActivity">
<LinearLayout
android:background="#f9f74922"
android:layout_width="match_parent"
android:layout_height="45dp">
<LinearLayout
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_width="10dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/back_btn"/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textColor="#ffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="登 陆"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="15dp">
</LinearLayout>
<LinearLayout
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="vertical">
<RelativeLayout
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="75dp">
<TextView
android:id="@+id/tv_number"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机:"/>
<EditText
android:id="@+id/login_iphone_et"
android:layout_toRightOf="@id/tv_number"
android:hint="请输入手机号"
android:background="#00000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/tv_number"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:background="#dedddd"
android:layout_width="match_parent"
android:layout_height="1dp" />
</RelativeLayout>
<RelativeLayout
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="75dp">
<TextView
android:id="@+id/tv_pwd"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditText
android:id="@+id/login_pwd_et"
android:layout_toRightOf="@id/tv_pwd"
android:hint="请输入密码"
android:background="#00000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/tv_pwd"/>
</RelativeLayout>
</LinearLayout>
<CheckBox
android:id="@+id/cb_pwd"
android:text="记住密码"
android:layout_width="match_parent"
android:layout_height="30dp"/>
<LinearLayout
android:layout_marginTop="30sp"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:textColor="#ffff"
android:background="#f9f74922"
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"
/>
<Button
android:textColor="#ffff"
android:background="#f9f74922"
android:id="@+id/btn_register"
android:layout_marginLeft="80dp"
android:layout_toRightOf="@id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
activity:
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class passwordActivity extends AppCompatActivity {
private EditText iphonenum,pwd;
private Button loginbtn;
private CheckBox checkBox;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_password);
iphonenum=findViewById(R.id.login_iphone_et);
pwd=findViewById(R.id.login_pwd_et);
loginbtn=findViewById(R.id.btn_login);
checkBox=findViewById(R.id.cb_pwd);
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
//CheakBox的状态
boolean isRemenber=sharedPreferences.getBoolean("checkBox",false);
//如果勾选了下次在进入就把保存的数据给读取出来
if (isRemenber==true){
String usename=sharedPreferences.getString("usename","");
String pwds=sharedPreferences.getString("pwds","");
iphonenum.setText(usename);
pwd.setText(pwds);
checkBox.setChecked(true);
}
//登陆的单击事件
loginbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String usename=iphonenum.getText().toString();
String pwds=pwd.getText().toString();
editor=sharedPreferences.edit();
//判断是否勾上了记住密码
if (checkBox.isChecked()){
editor.putString("usename",usename);
editor.putString("pwds",pwds);
editor.putBoolean("checkBox",true);
}
else {
editor.clear();
}
editor.commit();
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
});
}
}