SharedPreference存储实战之记住登陆账号密码

我会带着你远行 2022-08-18 02:05 415阅读 0赞
  1. 数据持久化就是指将那些内存中的瞬时数据保存到持久化设备中(如手机文件、数据库等),当关机,停电后,数据不丢失。 Android 系统中主要提供了三种方式用于实现数据持久化功能,分别是: 1、文件存储 2SharedPreference 存储 3、数据库存储。

今日使用SharedPreference存储实现记住登陆账号密码的功能:

效果图:

Center

activity_main.xml中代码:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="vertical" >
  5. <TextView
  6. android:id="@+id/textView1"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:text="登陆成功,进入主页面"
  10. android:textSize="30sp" />
  11. <Button
  12. android:id="@+id/forceExit"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:text="强制退出" />
  16. </LinearLayout>

MainActivity中代码:

  1. package com.demo.rememberaccountdemo;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. public class MainActivity extends BaseActivity {
  9. private Button forceExit;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. forceExit = (Button) findViewById(R.id.forceExit);
  15. forceExit.setOnClickListener(new OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. // TODO Auto-generated method stub
  19. // 销毁所有的活动
  20. ActivityCollector.finishAll();
  21. // 从主页面跳转到登录页面
  22. Intent intent = new Intent(MainActivity.this,
  23. LoginActivity.class);
  24. startActivity(intent);
  25. finish();
  26. }
  27. });
  28. }
  29. }

login.xml中代码:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent"
  4. android:orientation="vertical" >
  5. <LinearLayout
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content" >
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_weight="1"
  12. android:text="用户名:" />
  13. <EditText
  14. android:id="@+id/userName"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_weight="1"
  18. android:ems="10"
  19. android:hint="输入用户名" >
  20. <requestFocus />
  21. </EditText>
  22. </LinearLayout>
  23. <LinearLayout
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content" >
  26. <TextView
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_weight="1"
  30. android:text="密 码:" />
  31. <EditText
  32. android:id="@+id/password"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_weight="1"
  36. android:ems="10"
  37. android:hint="6~16位数字、密码" >
  38. <requestFocus />
  39. </EditText>
  40. </LinearLayout>
  41. <LinearLayout
  42. android:layout_width="match_parent"
  43. android:layout_height="wrap_content" >
  44. <CheckBox
  45. android:id="@+id/remember"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:text="记住密码" />
  49. </LinearLayout>
  50. <Button
  51. android:id="@+id/login"
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content"
  54. android:text="登陆" />
  55. </LinearLayout>

LoginActivity中的代码:

  1. package com.demo.rememberaccountdemo;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.os.Bundle;
  6. import android.preference.PreferenceManager;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.CheckBox;
  12. import android.widget.EditText;
  13. import android.widget.Toast;
  14. public class LoginActivity extends BaseActivity {
  15. private SharedPreferences preferences;
  16. private SharedPreferences.Editor editor;
  17. private EditText userNameText;
  18. private EditText passwordText;
  19. private CheckBox remember;
  20. private Button login;
  21. private String TAG = "LoginActivity" ;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.login);
  26. userNameText = (EditText) findViewById(R.id.userName);
  27. passwordText = (EditText) findViewById(R.id.password);
  28. remember = (CheckBox) findViewById(R.id.remember);
  29. login = (Button) findViewById(R.id.login);
  30. preferences = PreferenceManager.getDefaultSharedPreferences(this);
  31. boolean isRemember = preferences.getBoolean("remember", false);
  32. if (isRemember) {
  33. // 将账号和密码加载到文本框中
  34. String userName = preferences.getString("userName", "");
  35. String password = preferences.getString("password", "");
  36. userNameText.setText(userName);
  37. passwordText.setText(password);
  38. remember.setChecked(true);
  39. }
  40. login.setOnClickListener(new OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. // 获得用户名和密码
  44. String userName = userNameText.getText().toString();
  45. String password = passwordText.getText().toString();
  46. // 判断用户名和密码是否正确
  47. if (userName.equals("admin") && password.equals("123456")) {
  48. editor = preferences.edit();
  49. if (remember.isChecked()) {
  50. Log.d(TAG, "come in remember") ;
  51. // 检查复选框是否被选中,选中则将数据放到editor中
  52. editor.putBoolean("remember", true);
  53. editor.putString("userName", userName);
  54. editor.putString("password", password);
  55. } else {
  56. Log.d(TAG, " not come in remember") ;
  57. editor.clear();
  58. }
  59. editor.commit();// 提交数据
  60. // 登陆成功,跳转到主页面
  61. Log.d(TAG, " go to MainActivity") ;
  62. Intent intent = new Intent(LoginActivity.this,
  63. MainActivity.class);
  64. startActivity(intent);
  65. finish();
  66. } else {
  67. Toast.makeText(LoginActivity.this, "用户名密码错误", 1).show();
  68. }
  69. }
  70. });
  71. }
  72. }

由于页面的原因,ActivityCollector中的代码,BaseActivity中的代码,AndroidManifest.xml中的代码,就不贴出来了,想看的,下载demo里面有,

demo下载点这里:SharedPreference存储实战之记住登陆账号密码

保存前截图:

Center 1

保存后截图:

Center 2

保存值截图:

Center 3

发表评论

表情:
评论列表 (有 0 条评论,415人围观)

还没有评论,来说两句吧...

相关阅读