17.键值对数据存储SharedPreferences介绍

(1)介绍

SharedPreferences是一种轻量级的数据存储方式,采用键值对的存储方式。

SharedPreferences只能存储少量数据,大量数据不能使用该方式存储,支持存储的数据类型有booleans, floats, ints, longs, and strings。

SharedPreferences存储到一个XML文件中的

(2)向 Shared preferences 文件写入数据

​ 1)首先必须获得 SharedPreferences.Editor 对象;

​ 2)通过 putInt()putString() 等方式写入键值对数据;

​ 3)△最后通过调用 apply()commit() 方法保存数据

(3)从 Shared preferences 文件读取数据

首先获得文件对应的 SharedPreferences 对象;

通过 getInt()getString() 等方法指定 key 获取数据;

(4)学习使用 Shared preferences:登录时记住密码

登录布局源代码如下:

<?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=".MainActivity"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:layout_marginTop="48dp"><ImageViewandroid:layout_width="150dp"android:layout_height="150dp"android:layout_centerInParent="true"android:src="@drawable/ic_logo2"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="48dp"><ImageViewandroid:layout_width="32dp"android:layout_height="32dp"android:layout_marginBottom="4dp"android:layout_alignParentBottom="true"android:src="@drawable/ic_baseline_person_outline_24"/><EditTextandroid:id="@+id/et_Account"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_gravity="center"android:layout_marginStart="48dp"android:layout_marginEnd="48dp"android:layout_marginBottom="2dp"android:hint="Email/Account"android:inputType="text"android:maxLines="1" /></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="20dp"><ImageViewandroid:layout_width="32dp"android:layout_height="32dp"android:layout_marginBottom="4dp"android:layout_alignParentBottom="true"android:src="@drawable/ic_baseline_lock_24"/><EditTextandroid:id="@+id/et_pwd"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_gravity="center"android:layout_marginStart="48dp"android:layout_marginEnd="48dp"android:layout_marginBottom="2dp"android:hint="Password"android:inputType="textPassword"android:maxLines="1" /><ImageViewandroid:layout_width="24dp"android:layout_height="24dp"android:src="@drawable/ic_baseline_visibility_off_24"android:layout_marginBottom="6dp"android:id="@+id/iv_pwd_switch"android:layout_centerInParent="true"android:layout_alignParentEnd="true"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="16dp"><CheckBoxandroid:id="@+id/cb_remember_pwd"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentStart="true"android:layout_alignParentBottom="true"android:layout_marginLeft="8dp"android:layout_marginBottom="4dp"android:text="remeber password"android:textColor="@color/teal_700" /><TextViewandroid:text="sign up"android:textColor="@color/teal_700"android:id="@+id/tv_sign_up"android:layout_alignBaseline="@+id/cb_remember_pwd"android:layout_marginEnd="8dp"android:layout_width="wrap_content"android:layout_alignParentEnd="true"android:layout_alignParentBottom="true"android:layout_height="wrap_content"/></RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:layout_marginTop="48dp"><Buttonandroid:id="@+id/bt_login"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="32dp"android:text="Login"android:textColor="@color/white" /></RelativeLayout></LinearLayout>

MainActivity.java

package com.example.code05;import androidx.appcompat.app.AppCompatActivity;import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener{private EditText etPwd;private Boolean bPwdSwitch = false;private EditText etAccount;private CheckBox cbRemeberPwd;private Button btLogin;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final ImageView ivpwdswitch = findViewById(R.id.iv_pwd_switch);etPwd = findViewById(R.id.et_pwd);etAccount = findViewById(R.id.et_Account);cbRemeberPwd = findViewById(R.id.cb_remember_pwd);//获取string文件中的资源String spFileName = getResources().getString(R.string.shared_preferences_file_name);String accountKey = getResources().getString(R.string.login_account_name);String passwordKey = getResources().getString(R.string.login_password);String rememberPasswordKey = getResources().getString(R.string.login_remember_password);SharedPreferences spFile = getSharedPreferences(spFileName, MODE_PRIVATE);String account = spFile.getString(accountKey,null);String password = spFile.getString(passwordKey,null);//        String rememberPassword = spFile.getString(rememberPasswordKey,"false");boolean rememberPassword = spFile.getBoolean(rememberPasswordKey,false);if(account != null && !TextUtils.isEmpty(account)){etAccount.setText(account);}if(password != null && !TextUtils.isEmpty(password)){etPwd.setText(password);}cbRemeberPwd.setChecked(rememberPassword);//      密码明文转换ivpwdswitch.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {bPwdSwitch = !bPwdSwitch;if(bPwdSwitch){ivpwdswitch.setImageResource(R.drawable.ic_baseline_visibility_24);etPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);}else {ivpwdswitch.setImageResource(R.drawable.ic_baseline_visibility_off_24);etPwd.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD |InputType.TYPE_CLASS_TEXT);}}});btLogin = findViewById(R.id.bt_login);if (btLogin!=null) {btLogin.setOnClickListener(this);}}/*** 实现记住密码*/@Overridepublic void onClick(View v) {//        Toast.makeText(MainActivity.this, "Hello World!",
//                Toast.LENGTH_SHORT).show();String spFileName = getResources().getString(R.string.shared_preferences_file_name);String accountKey = getResources().getString(R.string.login_account_name);String passwordKey = getResources().getString(R.string.login_password);String rememberPasswordKey = getResources().getString(R.string.login_remember_password);SharedPreferences spFile = getSharedPreferences(spFileName ,Context.MODE_PRIVATE);SharedPreferences.Editor editor = spFile.edit();if(cbRemeberPwd.isChecked()){String password = etPwd.getText().toString();String account = etAccount.getText().toString();editor.putString(accountKey,account);editor.putString(passwordKey,password);editor.putBoolean(rememberPasswordKey,true);editor.apply();}else {editor.remove(accountKey);editor.remove(passwordKey);editor.remove(rememberPasswordKey);editor.apply();}}
}

18.布尔bool转变成String类

Bollean.getBollean(String s)

19.运行项目时总是出现Waiting for all target devices to come online

翻译成中文的意思是:等待所有目标设备上线

刚开始还以为是网络连接不通畅,后来百度一下发现不是。

可用的方法有:

(1).换一台运行模拟器

(2).打开AVD Manager,对相应的模拟器进行数据删除

android学习(4)相关推荐

  1. Android学习路线

    Android学习路线 第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环.  2.String和StringBuffer的使用.正则表达式.  3.面向对象的抽象,封装,继承 ...

  2. 一篇文章一张思维导图看懂Android学习最佳路线

    一篇文章一张思维导图看懂Android学习最佳路线 先上一张android开发知识点学习路线图思维导图 Android学习路线从4个阶段来对Android的学习过程做一个全面的分析:Android初级 ...

  3. Android学习系列(10)--App列表之拖拽ListView(上)

    研究了很久的拖拽ListView的实现,受益良多,特此与尔共飨.       鉴于这部分内容网上的资料少而简陋,而具体的实现过程或许对大家才有帮助,为了详尽而不失真,我们一步一步分析,分成两篇文章. ...

  4. Android学习第一书

    大家好,我是一名Facebook的工程师,同时也是<第一行代码--Android>的忠实读者. 虽然我最近几年是在国外读书和工作的,但是和很多人一样,我也非常喜欢郭霖的博客以及他写的< ...

  5. Android学习笔记21:ImageView获取网络图片

    Android平台有3种网络接口可以使用,它们分别是:java.net.*(标准java接口).org.apache(Apache接口)和android.net.*(Android网络接口).本文将使 ...

  6. 四、Android学习第四天——JAVA基础回顾(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 四.Android学习第四天--JAVA基础回顾 这才学习Android的 ...

  7. Android学习-- 基于位置的服务 LBS(基于百度地图Android SDK)--定位SDK

    原文:Android学习-- 基于位置的服务 LBS(基于百度地图Android SDK)--定位SDK 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.ne ...

  8. 我的Android学习体系

    我的Android学习之路历经坎坷啊,现在回过头来主要想分享下我学习Android开发所走过的过程中所学会的一些误区和弯路,那些让自己的进步一直很慢的原因,一直没有什么成就的原因,希望其他人可以借鉴我 ...

  9. 十一、Android学习第十天——项目开始(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 十一.Android学习第十天--项目开始 Android知识点的学习告一 ...

  10. Android学习笔记(七):多个Activity和Intent

    根据www.mars-droid.com:Andriod开发视频教学,先跳过书本<Beginning Android 2>的几个章,我是这两个资源一起看,需要进行一下同步.先初步了解一下应 ...

最新文章

  1. 2021年大数据Spark(四十一):SparkStreaming实战案例六 自定义输出 foreachRDD
  2. SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
  3. 大数据的发展,伴随的将是软件工程师的渐退,算法工程师的崛起
  4. 浙江大学-计算机中的数学(诙谐幽默的短视频)
  5. 初中计算机基础知识说课稿,计算机基础知识说课稿
  6. 【速看,双100%】剑指 Offer 14- I. 剪绳子 I
  7. 轨道车辆垂向振动Matlab建模与仿真,基于matlab/simulink的车辆建模与故障分析
  8. 详细分析GitLab CE 已遭在野利用漏洞 (CVE-2021-22205)
  9. 用报初会的照片报计算机,会计考试报名倒计时,手把手教你一次通过审核工具...
  10. 单片机学习方法总结资料分享
  11. 流利阅读12.31 The science of giving gifts your loved ones won’t want to return
  12. D3D9学习笔记之字体
  13. 分分钟穿透手机,实现远程控制
  14. netDxf实现对cad文件的读取与写入
  15. rx6600xt显卡相当于什么显卡
  16. 通过百度地图模糊查询获取详细地址?正则匹配
  17. Padding设置方法
  18. 深入浅出Python机器学习9——数据预处理、降维、特征提取及聚类
  19. Linux 两个文件求交集、并集、差集
  20. NotePlan for Mac如何修复iCloud同步问题

热门文章

  1. [转]为什么未来不需要我们?
  2. 牛皮席差异不同的地方!
  3. 原生JavaScript实现分页器
  4. 后端接收格式为x-www-form-urlencoded的数据
  5. 徐新说:创业是艰难的要懂得坚持住
  6. 如何写一个前端组件-以bootstrap-tab为例
  7. 刷题记录7.20 青蛙们跳台阶
  8. mysql字符集编码和排序规则
  9. 键盘鼠标(PS2)模拟器驱动及Demo
  10. GitHub分支创建及合并