【总结】Android攻城狮数据篇——SharedPreference

2024-02-26 21:58

本文主要是介绍【总结】Android攻城狮数据篇——SharedPreference,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android攻城狮数据篇—SharedPreference

Android的四种数据存储方式:
  SharedPreference
  SQLite
  Content Provider
  File

SharedPreferences简介

  SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。
实现Shared Preferences存储的步骤如下:
  (1)使用Activity类的getSharedPreferences方法获得
  SharedPreferences对象,其中存储key-value的文件的名称由getSharedPreferences方法的第一个参数指定。
  (2)使用SharedPreferences接口的edit获得SharedPreferences.Editor对象。
  (3)通过SharedPreferences.Editor接口的putXxx方法保存key-value对。其中XXX表示不同的数据类型。例如: 字符串类型的value需要用putString方法。
  (4)通过SharedPreferences.Edior接口的commit方法保存key-value对。commit方法相当于数据库事务中的提交(commit )操作。

具体实现代码

public class MainActivity extends AppCompatActivity {EditText put_name,put_word;CheckBox checkBox;SharedPreferences preferences;SharedPreferences.Editor editor;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);put_name = findViewById(R.id.put_name);put_word = findViewById(R.id.put_word);checkBox = findViewById(R.id.check);preferences = getSharedPreferences("myPref",MODE_PRIVATE);editor = preferences.edit();String name = preferences.getString("username","");if(name == null){checkBox.setChecked(false);}else{checkBox.setChecked(true);put_name.setText(name);}}public void DoClick(View view){switch (view.getId()){case R.id.login:String name = put_name.getText().toString().trim();String words = put_word.getText().toString().trim();if("admin".equals(name) &&"123456".equals(words)){if( checkBox.isChecked()){editor.putString("username", name);editor.putString("password",words);editor.commit();}else{editor.remove("username");editor.commit();}Toast.makeText(MainActivity.this, "登陆成功",Toast.LENGTH_SHORT).show();}else {Toast.makeText(MainActivity.this, "禁止登陆",Toast.LENGTH_SHORT).show();}break;;default:break;}}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="e.a17909.myapplication.MainActivity"><EditText
        android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentEnd="true"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:layout_toEndOf="@+id/username"android:layout_toRightOf="@+id/username"android:id="@+id/put_name" /><EditText
        android:id="@+id/put_word"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentEnd="true"android:layout_alignParentRight="true"android:layout_below="@+id/put_name"android:layout_toEndOf="@+id/password"android:layout_toRightOf="@+id/password" /><TextView
        android:id="@+id/username"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/put_name"android:layout_alignBottom="@+id/put_name"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:text="用户名" /><TextView
        android:id="@+id/password"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/put_word"android:layout_alignBottom="@+id/put_word"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:layout_toLeftOf="@+id/put_name"android:layout_toStartOf="@+id/put_name"android:text="密    码" /><CheckBox
        android:id="@+id/check"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:layout_below="@+id/put_word"android:text="记住用户名" /><Button
        android:id="@+id/login"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:onClick="DoClick"android:layout_below="@+id/check"android:text="登陆" /><Button
        android:id="@+id/cancel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/login"android:layout_alignBottom="@+id/login"android:layout_toEndOf="@+id/login"android:layout_toRightOf="@+id/login"android:text="取消" /></RelativeLayout>

这篇关于【总结】Android攻城狮数据篇——SharedPreference的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/750285

相关文章

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

Redis 中的热点键和数据倾斜示例详解

《Redis中的热点键和数据倾斜示例详解》热点键是指在Redis中被频繁访问的特定键,这些键由于其高访问频率,可能导致Redis服务器的性能问题,尤其是在高并发场景下,本文给大家介绍Redis中的热... 目录Redis 中的热点键和数据倾斜热点键(Hot Key)定义特点应对策略示例数据倾斜(Data S

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Python实现将MySQL中所有表的数据都导出为CSV文件并压缩

《Python实现将MySQL中所有表的数据都导出为CSV文件并压缩》这篇文章主要为大家详细介绍了如何使用Python将MySQL数据库中所有表的数据都导出为CSV文件到一个目录,并压缩为zip文件到... python将mysql数据库中所有表的数据都导出为CSV文件到一个目录,并压缩为zip文件到另一个

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现