设为首页 加入收藏

TOP

SharedPreferences实现保存用户名功能(一)
2017-10-12 17:48:14 】 浏览:5719
Tags:SharedPreferences 实现 保存 户名 功能

1. 简介

??SharedPreferences是一种轻型的数据存储方式,通过key-value键值对的方式将数据存储在xml文件中,常用于存储简单的配置信息。

2. 使用方式

2.1 获取SharedPreferences对象

??Android中可通过以下三种方式获取SharedPreferences对象:

2.2.1 Context类中的getSharedPreferences()

??接收两个参数,第一个参数指定存储数据的文件,若指定文件不存在,则新建该文件,存放目录为"/data/data/package_name/shared_prefs/",其中package_name为包名。

??第二个参数则为操作模式,主要有两种:

??MODE_PRIVATE:私有模式,默认情况下的模式,与直接传入0作为参数效果一样,表示只有当前程序可对这个文件进行操作。

??MODE_MULTI_PROCESS:多进程模式,允许多个进程对该文件进行操作。

2.2.2 Activity类中的getPreferences()

??这个方法与上一个方法比较相似,不同之处在于它只接收一个参数,用于指定操作模式,而无需指定文件名,这个方法默认将当前Activity的类名作为存储数据的文件名。

2.2.3 PreferenceManager类中的getDefaultSharedPreferences()

??这是一个静态方法,接收一个Context参数,使用当前应用程序的包名作为存储数据的文件名。

2.2 获取SharedPreferences.Editor对象

??SharedPreferences对象本身是只可以读取而不能保存数据的,需要保存数据则要调用SharedPreferences对象的edit()方法获取一个Editor对象。

2.3 通过putXxx方法存储数据

??得到Editor对象后,则可调用它的putXxx方法添加数据,这里的Xxx指的是添加的数据类型,例如存储字符串数据则调用putString()方法。这个方法接收两个参数,第一个参数为key值,第二个参数为数据的值,即一个键值对。

2.4 提交变化

??添加或移除(remove方法)数据后,需要调用Editor对象的commit()方法将所作变化提交。

2.5 获取存储的数据

??获取已经存储的数据较为简单,直接调用SharedPreferences对象的getXxx方法即可,使用方法与Editor对象的putXxx类似。这个方法也是接收两个参数,第一个参数指定要获取的数据的key值,第二个参数指定当获取的数据不存在时所返回的默认值。

3. 范例-实现保存用户名的功能

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context="com.studying.myapplication.MainActivity">

    <!--用户名-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="用户名" />

        <EditText
            android:id="@+id/username"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4" />
    </LinearLayout>

    <!--密码-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="密码" />

        <EditText
            android:id="@+id/passward"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:inputType="textPassword" />
    </LinearLayout>

    <!--是否记住用户名-->
    <CheckBox
        android:id="@+id/remember"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇自定义控件详解(三):Canvas效.. 下一篇Xamarin android 的WebClient Jso..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目