Android 关于数据存储的小案例——SharedPreferences存储方式

           SharedPreferences存储方式是Android平台上一个轻量级的存储类,主要是用于存储一些应用程序的配置参数。例如用户名、密码、自定义参数的设置等。SharedPreferences中存储的数据是以key/value键值对的形式保存在XML文件中,该文件位于data/data/<packagename>/shared_prefs文件夹中。需要注意的是:SharedPreferences中的value值只能是float、int、long、boolean、String、StringSet类型数据。

         在使用SharedPreferences存储方式进行存储,首先要通过context.getSharedPreferences(Stringname、int、mode)获取SharedPreferences的实例对象(在Activity中可以直接使用this代表上下文,如果不是则需要传一个Context对象获取上下文)。


我这次演示的小案例如下图:



 具体实现步骤如下:

一、首先要先进行界面布局的设置,观察布局需要的控件,准备好布局需要的图片,调整布局的美观设置,使布局看起来更加人性化,更加富有美感。

    

    上面这是布局文件。各个布局的代码如下:

1、activity_login.xml布局代码:

<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:id="@+id/activity_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/loginbg"
    tools:context="com.example.administrator.case_login.LoginActivity">

    <include layout="@layout/login_top"></include>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值