工程目录:

MainActivity

package com.example.demo_eight;
import android.app.Activity;
import android.content.SharedPreferences;
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;import androidx.appcompat.app.AppCompatActivity;import com.example.demo_eight.R;public class MainActivity extends AppCompatActivity {private EditText et1, et2;SharedPreferences sp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et1 = findViewById(R.id.edit1);et2 = findViewById(R.id.edit2);sp = getPreferences(Activity.MODE_PRIVATE);String username = sp.getString("username", "");  //第2参数表示按键名取不到值时,设置为空串String password = sp.getString("password", "");et1.setText(username);et2.setText(password);Button btn1=findViewById(R.id.btn1);btn1.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {String s1 = et1.getText().toString();String s2 = et2.getText().toString();Toast.makeText(getApplicationContext(), "输入的用户名为" + s1 + ",输入的密码为" + s2, Toast.LENGTH_LONG).show();}});Button btn2=findViewById(R.id.btn2);btn2.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v) {et1.setText("");et2.setText("");Toast.makeText(getApplicationContext(), "取消登录", Toast.LENGTH_SHORT).show();}});CheckBox cb = findViewById(R.id.cb);cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1) {if (arg1) {Toast.makeText(MainActivity.this, "已保存登录信息", Toast.LENGTH_LONG).show();sp.edit().putString("username", et1.getText().toString()).putString("password", et2.getText().toString()).commit();} else {Toast.makeText(MainActivity.this, "不保存登录信息", Toast.LENGTH_LONG).show();sp.edit().putString("username", null).putString("password", null).commit();}}});}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户名:" /><EditTextandroid:id="@+id/edit1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:ems="10"android:inputType="textPersonName"android:text="" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/textView4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密    码:" /><EditTextandroid:id="@+id/edit2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:ems="10"android:inputType="textPersonName"android:text="" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btn1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="登录" /><Buttonandroid:id="@+id/btn2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="取消" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="vertical"><CheckBoxandroid:id="@+id/cb"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="记住我" /></LinearLayout></LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>


Fragment内:

 @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_home ,container, false);sp=getActivity().getSharedPreferences("Tips",Context.MODE_PRIVATE);sp.edit().putString("tips","两只起死回生手,一颗安民济世心。").commit();TextView tv2=(TextView) view.findViewById(R.id.tv_home_2);tv2.setText(sp.getString("tips",""));return view;}


List类型:
存储(存放key为size,value为该数组大小值,其它为key为一个字符串加数组index,value为数组对应的值)

private List<String> outcomeitemList;`
SharedPreferences sp = this.getSharedPreferences("data", Context.MODE_PRIVATE);SharedPreferences.Editor edit = sp.edit();edit.putInt("size", outcomeitemList.size());for (int i = 0; i < outcomeitemList.size(); i++) {edit.putString("item" + i, outcomeitemList.get(i));}edit.commit();

取出:

SharedPreferences sp=this.getSharedPreferences("data",Context.MODE_PRIVATE);int size=sp.getInt("size",0);for(int i=0;i<size;i++) {outcomeitemList.add ( sp.getString("item"+i,null));}

Android的SharedPreferences存取String和List<String>类型(在Activity和Fragment内使用)相关推荐

  1. android 小卷毛播放器,Android 音乐播放器的开发教程(四)Activity和Fragment的通信以及Fragment的切换 ----- 小达...

    Activity和Fragment的通信以及Fragment的切换 在上一篇的博客中讲到了,播放器的主界面布局,是由一个activity和一个fragment构成的,activity启动的时候,在其o ...

  2. Android肝帝战纪之Fragmentation的使用(单Activity+多Fragment设计)

    #单Activity+多Fragment设计(Fragmentation的使用)# 本文介绍使用Fragmentation库打造单Activity+多Fragment的App基础架构: Fragmen ...

  3. 《Android/OPhone开发完全讲义》连载(7):使用SharedPreferences存取复杂数据

    本文为<Android/OPhone开发完全讲义>一书的内容连载.转载请注明出处 我们知道SharedPreferences只能保存简单类型的数据,例如,String.int等.如果想用S ...

  4. Android开发笔记(二十九)使用SharedPreferences存取数据

    SharedPreferences使用场景 共享参数(SharedPreferences)是Android上的一个轻量级存储工具,存储结构是类似map的key-value键值对形式.它主要用于保存ap ...

  5. android中SharedPreferences和PreferenceActivity的存取数据

    本文主要介绍SharedPreferences和PreferenceActivity的基础知识和用法. 主要资料来源于网络,包括但不限于: <Android之PreferenceActivity ...

  6. C++ STL笔记二:string容器;string构造函数;string赋值函数;string拼接;string查找、替换;string比较;string单个字符存取;string插入、删除、截取

    本质: string是C++风格的字符串,而string本质上是一个类. string和char*区别: char*是一个指针 string是一个类,类内部封装了char*,管理这个字符串,是一个ch ...

  7. android 多行 输入框,EditTextView Android中的多行字符串(Multiline String in EditTextView Android)...

    EditTextView Android中的多行字符串(Multiline String in EditTextView Android) 我用这段代码创建了我的EditTextView: mEdit ...

  8. Android之SharedPreferences 讲解

    <pre code_snippet_id="220451" snippet_file_name="blog_20140306_1_1587302" cla ...

  9. Android之SharedPreferences两个工具类

    相信Android的这个最简单的存储方式大家都很熟悉了,但是有一个小小技巧,也许你没有用过,今天就跟大家分享一下,我们可以把SharedPreferences封装在一个工具类中,当我们需要写数据和读数 ...

最新文章

  1. Python+OpenCV 图像处理系列(4)—— 图像像素的读写、算术运算、逻辑运算及像素的统计
  2. 基于 OpenCV 的表格文本内容提取
  3. 一套基于 SpringBoot 的完整高并发电商系统,拿去面试简直不要太好用!
  4. PAT1123 Is It a Complete AVL Tree(AVL树完全二叉树)
  5. Java实现 简单聊天软件
  6. ELF文件格式与进程地址空间的联系
  7. BZOJ2212——线段树合并
  8. Unity新手教程:Roll-a-Ball游戏开发中的使用
  9. TOJ 4393 Game
  10. logstash5.x改变
  11. 国贸专业要考计算机二级,国贸专业考计算机二级有没有必要
  12. css-阴影和超链接伪类
  13. activiti 流程文件存哪里_JAVA-工作流引擎-activiti-Tasks介绍
  14. python如何确定拐点_如何确认均线拐点的实战技巧和理论(图解)
  15. 一文了解 caffe 框架 | CSDN 博文精选
  16. 为什么只有软件就可以用盗版?
  17. kotlin 类构造函数_Kotlin类– Kotlin构造函数
  18. OFDM链路自适应算法 注水算法简单c++实现
  19. 腾达无线路由器dns服务器,腾达路由器怎么手动设置dns地址
  20. 智商一般学计算机,IQ最高的十大专业公布 智商不高慎选

热门文章

  1. 比特币区块链将分道扬镳、Libra苦难继续,2020区块链进入关键时期!
  2. 终于!孙宇晨和巴菲特吃上 3153 万元的晚餐,还送了一个比特币!
  3. 开发者强势围观!Gartner 发布 2020 年十大战略科技发展趋势
  4. 华为否认今年将推出搭载鸿蒙系统手机;苹果或在 3 年内推出 5G 基带芯片;Node.js 12.12.0 发布 | 极客头条...
  5. “遗留代码是传奇!”
  6. 华为否认削减手机产量;百度副总裁郑子斌将离职;开发者对苹果发集体诉讼 | 极客头条...
  7. TIOBE 5 月编程语言排行榜:Python、C++ 竞争白热化,Objective-C 已沦为小众语言
  8. 雷军:全面屏新零售微信都是我先提的,为什么你们只记住了苹果马云张小龙?
  9. javaweb实训第二天上午——jQuery基础
  10. c语言期末读程序,C语言期末程序.doc