使用SharedPreferences保存用户名和密码
一,什么是SharedPreferences?
二.SharedPreferences怎么用?
1.获得使用SharedPreferences对象;
2.获得Editor对象;
3.通过Editor对象的putXXX函数,设置写入数据;
4.通过Editor对象的commit()提交写入
三.SharedPreferences保存用户名和密码代码实例:
xml中代码:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.administrator.myapplicat321.Main2Activity">
<EditText
android:id="@+id/s1"
android:layout_width="match_parent"
android:layout_height="50dp" />
<EditText
android:id="@+id/s2"
android:layout_width="match_parent"
android:inputType="textPassword"
android:layout_height="50dp" />
<CheckBox
android:id="@+id/zx"
android:text="记住密码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="登录"
android:layout_gravity="center"
/>
</LinearLayout>
activity中代码:
package com.example.administrator.myapplicat321;
import android.app.Activity;
import android.content.SharedPreferences;
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;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
private EditText editText1;
private EditText editText2;
private Button button;
private String name;
private String password;
private CheckBox checkBox;
private int rememberFlag = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
editText1 = findViewById(R.id.s1);
editText2 = findViewById(R.id.s2);
button = findViewById(R.id.btn);
checkBox = findViewById(R.id.zx);
onClick_ReadData();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name = editText1.getText().toString();//获取edittext中输入的消息
password = editText2.getText().toString();
onClick_WriteData();
}
});
}
public void onClick_WriteData() {
SharedPreferences mySharedPreferences = getSharedPreferences("test",
MODE_PRIVATE);//创建文件
SharedPreferences.Editor editor = mySharedPreferences.edit();//创建editor对象,写入值
editor.putString("name", name);//保存用户名
if (checkBox.isChecked()){//判断是否勾选单选框
rememberFlag=1;//勾选的话为1,则显示密码
editor.putInt("remember_flag", rememberFlag);
editor.putString("password", password);
}else {
rememberFlag=0;
editor.putInt("remember_flag", rememberFlag);
}
editor.commit();//提交值
Toast.makeText(this, "数据成功写入SharedPreferences!",
Toast.LENGTH_LONG).show();
}
public void onClick_ReadData() {
SharedPreferences sharedPreferences = getSharedPreferences("test",
MODE_PRIVATE);//从ps中提取文件
if (sharedPreferences != null) {//判断写入值是否为空
name = sharedPreferences.getString("name", "");
password = sharedPreferences.getString("password", "");//取出密码
rememberFlag = sharedPreferences.getInt("remember_flag", 0);//取出单选框的值
editText1.setText(name);
}
if (rememberFlag==1){
checkBox.setChecked(true);
editText2.setText(password);
}
}
}