sharepreference实现记住密码功能

待我称王封你为后i 2022-08-02 00:21 321阅读 0赞

SharePreference是用于保存数据用的,主要调用Context.getSharePreferences(String name, int mode)方法来得到SharePreferences接口,该方法的第一个参数是文件名称,第二个参数是操作模式。

操作模式有三种:

MODE_PRIVATE(私有)

MODE_WORLD_READABLE(可读)

MODE_WORLD_WRITEABLE(可写)

  1. **SharePreference提供了获得数据的方法,如getString(String key,String defValue)等,****调用harePreferencesedit()方法返回SharePreferences.Editor内部接口,该接口提供了保存数据的方法如:putString(String key,String value)等,调用该接口的commit()方法可以将数据保存。**

效果图如下:

Center

主要xml代码:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/bg_login_activity"
  6. android:orientation="vertical"
  7. tools:context=".MainActivity" >
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_marginLeft="35dip"
  12. android:layout_marginTop="150dip"
  13. android:orientation="horizontal" >
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="用户名:"
  18. android:textSize="20dp" />
  19. <EditText
  20. android:id="@+id/username"
  21. android:layout_width="200dp"
  22. android:layout_height="35dp"
  23. android:background="@drawable/bg_input_center" />
  24. </LinearLayout>
  25. <LinearLayout
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:layout_marginLeft="35dip"
  29. android:layout_marginTop="8dp"
  30. android:orientation="horizontal" >
  31. <TextView
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:text=" 密码:"
  35. android:textSize="20dp" />
  36. <EditText
  37. android:id="@+id/password"
  38. android:layout_width="200dp"
  39. android:layout_height="35dp"
  40. android:background="@drawable/bg_input_center"
  41. android:password="true" />
  42. </LinearLayout>
  43. <LinearLayout
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:layout_marginLeft="75dip"
  47. android:layout_marginTop="8dp"
  48. android:orientation="horizontal" >
  49. <TextView
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:text="记住密码:" />
  53. <CheckBox
  54. android:id="@+id/savePassword"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content" />
  57. </LinearLayout>
  58. <Button
  59. android:id="@+id/login_btn"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:layout_marginLeft="75dip"
  63. android:text="登陆" />
  64. </LinearLayout>
  65. </LinearLayout>

保存数据到文件的主要函数:

  1. public void setUserInfo(String key, String value) {
  2. SharedPreferences sp = context.getSharedPreferences(USER_INFO,
  3. Context.MODE_PRIVATE);
  4. SharedPreferences.Editor editor = sp.edit();
  5. editor.remove(key);
  6. editor.putString(key, value);
  7. 26 editor.commit();
  8. 27 }

发表评论

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

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

相关阅读