一、什么是SharePreferences
1、SharePreferences常用来存储一些轻量级的数据,
2、以key-value形式存储数据,可以存储的数据类型为string、float、int 、long 、boolean。
3、存储位置在/data/data/<包名>/shared_prefs目录下。
4、保存的数据以xml存储。
二、使用SharePreferences写入数据步骤
01、获得使用SharePreferences对象;
02、获得Editor对象;
03、通过Editor对象的putxxx函数,设置写入数据;
04、通过Editor对象的commit()提交写入。
三、获取SharePreferences对象
SharePreferences对象:在有Context坏境中使用getSharePreferences方法获得
getSharePreferences方法的参数:1、name:要保存的文件名 ;2、mode:Context.MODE_PRNATE(默认操作模式);Context.MODE_APPEND(该模式会检查文件是否存在,存在就往文件里面追加内容,否则就创建新文件);
三、使用SharePreferences获取数据
- 获得使用SharePreferences对象;
- 通过SharePreferences对象的getxxx函数,获得对应类型的数据;
四、使用SharePreferences获取数据案例
实例:账号 密码 点击记住密码法返回时依然存在
//布局文件
<EditText
android:id="@+id/main_edit1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="账户"/>
<EditText
android:id="@+id/main_edit2"
android:layout_width="match_parent"
android:inputType="numberPassword"
android:layout_height="60dp"
android:hint="密码"/>
<Button
android:id="@+id/main_btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="登录"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
//复选框,可勾选
<CheckBox
android:id="@+id/main_check1"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:id="@+id/main_text1"
android:layout_width="100dp"
android:layout_height="60dp"
android:text="记住密码"/>
<CheckBox
android:id="@+id/main_check2"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:id="@+id/main_text2"
android:layout_width="100dp"
android:layout_height="60dp"
android:text="自动登录"/>
</LinearLayout>
//activity中
public class MainActivity extends AppCompatActivity {
//定义xml中写的控件
private EditText usernameEt;
private EditText userpassEt;
private Button loginBtn;
private CheckBox rememberPswCK;
private CheckBox autologinCK;
private TextView textView1;
private TextView textView2;
//定义标志位
private int rememberFlag;
private String password="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindID();
//从sp文件(mysp.xml)取出“name”节点对应的值
SharedPreferences sharedPreferences=getSharedPreferences("zxc.xml",MODE_PRIVATE);
//判断sharedPreferences对象是否为空
if (sharedPreferences!=null){
String name=sharedPreferences.getString("name","");
password=sharedPreferences.getString("password","");
rememberFlag=sharedPreferences.getInt("remember_flag",0);
//赋值给usernameEt
usernameEt.setText(name);
}
//复选框在关掉时是否被选中(一直选中)
if (rememberFlag==1){
rememberPswCK.setChecked(true);
userpassEt.setText(password);
}
//设置监听事件
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//EditText的内容转换成String格式
String username=usernameEt.getText().toString();
String userpass=userpassEt.getText().toString();
//1、创建SharedPreferences对象
SharedPreferences sharedPreferences=getSharedPreferences("zxc.xml",MODE_PRIVATE);
//2、创建Editor对象,写入值
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("name",username);
//判断复选框是否选中
if(rememberPswCK.isChecked()){
rememberFlag=1;
editor.putInt("remember_flag",rememberFlag);
editor.putString("password",userpass);
}else {
rememberFlag=0;
editor.putInt("remember_flag",rememberFlag);
}
editor.commit();//提交
Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
}
});
}
private void bindID() {
usernameEt=findViewById(R.id.main_edit1);
userpassEt=findViewById(R.id.main_edit2);
loginBtn=findViewById(R.id.main_btn);
rememberPswCK=findViewById(R.id.main_check1);
textView1=findViewById(R.id.main_text1);
autologinCK=findViewById(R.id.main_check2);
textView2=findViewById(R.id.main_text2);
}
}