1.什么是SharedPreferences 存储?

SharedPreferences是Android平台上一个轻量级的存储类,用来存储少量数据时简单,便捷(保存记住密码状态,设置开关状态等)。
key-value(键值对)形式存储数据,可以存储的数据类型为:String、float、int、long、boolean。
存储位置在/data/data/<包名>/shared_prefs目录下。
保存的数据以XML形式存储。

2.使用SharedPreferences写入数据步骤:

1.获得使用SharedPreferences对象;
2.获得Editor对象;
3.通过Editor对象putXXX函数,设置写入数据;
4.通过Editor对象的commit提交写入;

获取SharePreferences对象

在有Context环境中使用getSharePreferences方法获得

获取Editor对象

通过SharePreferences.edit()方法获得

使用SharedPreferences存储数据案例

package com.example.myapplication15;import android.content.Context;
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.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {private EditText edit_username;private EditText edit_password;private CheckBox checkBox;private Button button;private CheckBox checkBox_zidong;private int remamberFlag = 0;private String password = "";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);binID();SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);//如果不为空if (sharedPreferences != null) {String name = sharedPreferences.getString("name", "");password = sharedPreferences.getString("password", "");remamberFlag = sharedPreferences.getInt("remeber_flag", 0);edit_username.setText(name);}//确定为1获取 记住密码,打钩if (remamberFlag == 1) {checkBox.setChecked(true);edit_password.setText(password);}button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//1 创建 SharePreferences 对象String username = edit_username.getText().toString();String password = edit_password.getText().toString();SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);SharedPreferences.Editor editor = sharedPreferences.edit();//2  创建Editor对象,写入值editor.putString("name", username);if (checkBox.isChecked()) {remamberFlag = 1;editor.putInt("remeber_flag", remamberFlag);editor.putString("password", password);} else {remamberFlag = 0;editor.putInt("remeber_flag", remamberFlag);}//3  提交editor.commit();Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();}});}private void binID() {edit_username = findViewById(R.id.edit_username);edit_password = findViewById(R.id.edit_password);checkBox = findViewById(R.id.Cb);checkBox_zidong = findViewById(R.id.zidong_check);button = findViewById(R.id.main_button);}
}

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.myapplication15.MainActivity"><RelativeLayout
        android:layout_marginTop="200dp"android:gravity="center"android:layout_gravity="center"android:layout_width="match_parent"android:layout_height="wrap_content"><EditText
            android:id="@+id/edit_username"android:hint="输入账号"android:layout_gravity="center"android:layout_width="250dp"android:layout_height="50dp" /></RelativeLayout><RelativeLayout
        android:gravity="center"android:layout_gravity="center"android:layout_width="match_parent"android:layout_height="wrap_content"><EditText
            android:id="@+id/edit_password"android:hint="输入密码"android:password="true"android:layout_gravity="center"android:layout_width="250dp"android:layout_height="50dp" /></RelativeLayout><Button
        android:id="@+id/main_button"android:layout_width="match_parent"android:layout_height="40dp"android:text="登录"/><LinearLayout
    android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="40dp"><CheckBox
        android:id="@+id/Cb"android:layout_width="30dp"android:layout_height="20dp"android:layout_marginLeft="60dp"/><TextView
        android:textColor="#000000"android:layout_width="80dp"android:layout_height="30dp"android:text="记住密码"/><CheckBox
        android:id="@+id/zidong_check"android:layout_width="30dp"android:layout_height="20dp"android:layout_marginLeft="60dp"/><TextView
        android:textColor="#000000"android:layout_width="80dp"android:layout_height="30dp"android:text="自动登录"/>
</LinearLayout></LinearLayout>

用 SharedPreferences 存储进行记住密码 和保存用户名(记住密码)相关推荐

  1. linux git 记录密码,linux git 保存用户名和密码

    一.通过文件方式 1.在~/下, touch创建文件 .git-credentials, 用vim编辑此文件,输入内容格式: touch .git-credentials vim .git-crede ...

  2. android 保存 用户名和密码 设置等应用信息优化

    1.传统的保存用户名,密码方式 SharedPreferences Editor editor = shareReference.edit(); editor.putString(KEY_NAME,& ...

  3. 如何在Git中保存用户名和密码?

    本文翻译自:How to save username and password in Git? I want to use a push and pull automatically in GitEx ...

  4. Android复习05【网络编程---WebView获取文章信息、保存用户名与密码、设置菜单样式、收藏文章列表】

    2020-04-02-星期四[源码可私聊我,QQ:386335886] 写篇文章不容易,点个赞再走吧,求求了~  目   录 网络访问-思维导图 玩Android网站---查看登录Cookie 适配器 ...

  5. Android保存用户名和密码

    我们不管在开发一个项目或者使用别人的项目,都有用户登录功能,为了让用户的体验效果更好,我们通常会做一个功能,叫做保存用户,这样做的目地就是为了让用户下一次再使用该程序不会重新输入用户名和密码,这里我使 ...

  6. iOS 开发 -- 使用KeyChain保存用户名、密码并实现自动登录

    一.前言 我的话,只是写了个keychain使用的工具类,让我们使用的时候可以直接调用接口,以求方便. 但是关于keychain的一些概念还有一些官方API我都不打算说的,当然你要看下面一些东西的话可 ...

  7. git用户名和密码保存文件_git保存用户名和密码

    git保存用户名和密码 如果只是让git保存用户名的话可以使用下面你的命令 git config –global user.name testuser 这样每次你和remote的repository操 ...

  8. Web项目,要求:保存用户名和密码在Cookie中,下次登录不再重新输入

    设计一个实现登录功能的Web项目,要求:保存用户名和密码在Cookie中,下次登录不再重新输入 var cookie = {};//设置 cookie.SetCookies=function(name ...

  9. iOS中Keychain保存用户名和密码

    摘要: 有用户就用用户名和密码,而现在的应用都少不了一个保存用户名和密码用于自动登录的功能,本文介绍使用iOS自带的Keychain方法保存用户名和密码. 说到保存用户名和密码,以前有用过本地的数据库 ...

  10. mstsc保存用户名和密码,实现自动登录远程桌面

    mstsc保存用户名和密码,实现自动登录远程桌面 https://blog.csdn.net/qq_23587541/article/details/82942365 MSTSC参数说明 首先可以使用 ...

最新文章

  1. 多线程批量拆分List导入数据库
  2. Python环境的安装(Anaconda+Jupyter notebook+Pycharm)
  3. 160个Crackme006
  4. .NET 6 中的七个 System.Text.Json 特性
  5. Android实现QQ登录
  6. python 自动化发送邮件_干货 | 解放双手,用Python实现自动发送邮件
  7. 两种方案实现word转pdf
  8. 【愚公系列】2022年10月 微信小程序-电商项目-商品详情页面说明和商品导航
  9. LabVIEW色彩匹配实现颜色识别、颜色检验(基础篇—13)
  10. 跑三小时的monkey测试该怎么算_Android命令Monkey压力测试,详解
  11. 《牛奶可乐经济学》读书笔记
  12. hive 安装mysql报错_hive的元数据存储在mysql后,报错的解决方法
  13. 切勿妄谈Hadoop,以及4个数据管道打造实践
  14. matplotlib设置坐标轴
  15. 微软修复打印机服务漏洞 所有支持Windows系统都受影响
  16. mysql网络数据库操作模块_15.mysql数据库操作与Paramiko模块
  17. 小程序实战—答题类小程序
  18. android 陀螺仪滤波_Arduino+mpu6050陀螺仪运用卡尔曼滤波姿态解算实验
  19. 三网融合可借鉴欧盟视听新媒体内容规制
  20. McAfee白名单设置

热门文章

  1. [发布]看天气WeatherCan V1.0 ---气象数据分析系统web版
  2. 页面传值、plusready、自定义事件
  3. 通过Response返回Json格式数据给前端
  4. 隔空传情: emoji 简史
  5. ubuntu官方容器更换清华apt源
  6. AD软件PCB导出Gerber文件有多余的丝印/3D能看到
  7. SQLServer JDBC 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接
  8. 【C语言】getchar与putchar混合输出
  9. 互联网摸鱼日报(2023-05-21)
  10. 醉了,Vue 3.0 公开代码之后……