【总结】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

相关文章

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转