SharedPreferences实现记住密码功能

时间:2023-11-22 12:06:20

SharedPerferences 简单介绍

  • 用于保存简单的键值对数据;
  • 它将数据放在 /data/data/<package name>/shared_prefs目录下,用xml文件保存MAP键值对;

SharedPerferences 使用步骤

将数据存储到SharedPerferences中:

  1.先要得到SharedPerference对象:(三种方法)

      1).使用Context类中的 getSharedPreferences() 方法,它接收两个参数,第一个参数为文件名称,第二个参数为操作模式。

       操作模式MODE_PRAVITE :只有当前程序才能对这个文件进行读写。MODE_MULTI_PROCESS :多个进程中对同一个文件进行读写。

       如:

SharedPreferences spf = getSharedPreferences("data",Context.MODE_PRIVATE);

      2).使用Activity类中的 getPreferences() 方法,它只接收一个参数--操作模式,并且会将当前活动的类名作为文件名。

       如:

SharedPreferences spf = getPreferences(MODE_PRIVATE);

      3).使用PreferenceManager类中的 getDefaultSharedPreferences() 方法,它接收一个Context参数,并且用包名作为前缀来命名文件。

       如:

SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(this);

  2.再得到SharedPreferences.Editor对象:

      使用SharedPreferences对象的 edit() 方法。

SharedPreference.Editor editor = spf.edit();

  3.开始添加数据:

      以键值对的方式写入数据。

editor.putInt("age",22);
editor.putString("name","Visen");
editor.putBoolean("singleDog",false)

  4.提交操作:

editor.commit();

从SharedPerferences中读取数据:

  提供了一系列的get方法进行数据的读取。如:

String name = editor.getString("name"," ");

  如果键所对应的值不存在,则填入设定的默认值。

简单的保存密码功能

login.xml 登录布局页面

SharedPreferences实现记住密码功能

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
android:stretchColumns="1"> <LinearLayout
android:layout_height="wrap_content"
android:background="@color/black"
android:orientation="vertical"> <ImageView
android:layout_width="match_parent"
android:layout_height="240dp"
android:src="@drawable/image1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title"
android:textSize="40sp"
android:textColor="@color/red"
android:gravity="center"
android:background="@color/cyan"/>
</LinearLayout> <TableRow
android:layout_marginTop="30dp">
<TextView
android:layout_height="wrap_content"
android:text="@string/account"
android:textSize="30sp"
android:textColor="@color/white"/>
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:inputType="text"
android:textSize="20sp"
android:textColor="@color/red"
android:gravity="center"
android:singleLine="true"/>
</TableRow> <TableRow>
<TextView
android:layout_height="wrap_content"
android:text="@string/password"
android:textSize="30sp"
android:textColor="@color/white"/>
<EditText
android:id="@+id/passWord"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textSize="20sp"
android:textColor="@color/red"
android:gravity="center" />
</TableRow> <TableLayout
android:layout_height="wrap_content"
android:stretchColumns="0"> <TableRow>
<CheckBox
android:id="@+id/saveSelect"
android:background="@color/red"
android:layout_gravity="end"/>
<TextView
android:layout_height="wrap_content"
android:text="@string/saveSelect"
android:textSize="20sp"
android:textColor="@color/white"
android:gravity="center"
android:layout_gravity="bottom"/>
</TableRow> <TableRow>
<Button
android:layout_height="wrap_content"
android:id="@+id/login"
android:gravity="center"
android:layout_span="2"
android:text="@string/login"
android:textSize="25sp"
android:textColor="@color/red"
android:background="@drawable/black_bt"/>
</TableRow>
</TableLayout>
</TableLayout>

Login.java

public class Login extends AppCompatActivity {

    private SharedPreferences spf;
private SharedPreferences.Editor spfe; private int num = 0;
private EditText account = null;
private EditText passWord = null;
private CheckBox saveSelect = null;
private Button login = null ; @Override
protected void onCreate(Bundle saveInstanceState){ //加载布局
super.onCreate(saveInstanceState);
setContentView(R.layout.login); //初始化控件
account = (EditText)findViewById(R.id.account);
passWord = (EditText)findViewById(R.id.passWord);
saveSelect = (CheckBox)findViewById(R.id.saveSelect);
login = (Button)findViewById(R.id.login); //使用Context的getSharedPreferences(String name,int mode)方法得到SharedPreferences对象;
spf = getSharedPreferences("data", Context.MODE_PRIVATE);
//使用SharedPreferences对象的edit()方法得到 SharedPreferences.Editor 的对象;
spfe = spf.edit(); //复选框是否被选中,若为选中状态,则保存过账户,要恢复数据
if(spf.getBoolean("isSelect",false)){//选中标志,默认值为false
String acc = spf.getString("account","");
String pas = spf.getString("passWord","");
account.setText(acc);
passWord.setText(pas);
saveSelect.setChecked(true);
} //设置登录按钮监听事件
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//确认帐号密码
if(account.getText().toString().equals("visen") && passWord.getText().toString().equals("dsy402645063!")){ //复选框是否被勾选,若被勾选,则需要保存账户后登录;否则直接登录且不保存账户
if(saveSelect.isChecked()){
saveDate();
}else {
spfe.clear();
spfe.commit();
} //页面跳转
Intent intent = new Intent(Login.this,MainActivity.class);
startActivity(intent);
finish();
}else {//账户或密码错误
Toast.makeText(Login.this, "account or password is invalid", Toast.LENGTH_SHORT).show();
}
}
}); } public void saveDate(){ //读取EditText中的内容
String acc = account.getText().toString();
String pas = passWord.getText().toString();
//保存数据
spfe.putString("account",acc);
spfe.putString("passWord",pas);
spfe.putBoolean("isSelect",true);
//提交
spfe.commit();
} @Override
public void onBackPressed(){
num++;
if(num == 2){
super.onBackPressed();
}else{
Toast.makeText(Login.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
}
}
}

SharedPreferences实现记住密码功能SharedPreferences实现记住密码功能