设为首页 加入收藏

TOP

SharedPreferences实现记住密码功能(一)
2017-10-13 09:57:13 】 浏览:4615
Tags:SharedPreferences 实现 记住 密码 功能

SharedPerferences 简单介绍

  • 用于保存简单的键值对数据;
  • 它将数据放在 /data/data/<package name>/shared_prefs目录下,用xml文件保存MAP键值对

SharedPerferences 使用步骤

将数据存储到SharedPerferences中:

  1.先要得到SharedPerference对象:(三种方法)

      1).使用Context类中的 getSharedPreferences() 方法,它接收两个参数,第一个参数为文件名称,第二个参数为操作模式。

       操作模式MODE_PRAVITE :只有当前程序才能对这个文件进行读写。MODE_MULTI_PROCESS :多个进程中对同一个文件进行读写。

       如:

SharedPreferences spf = getSharedPreferences("data",Context.MODE_PRIVATE);

 

      2).使用Activity类中的 getPreferences() 方法,它只接收一个参数--操作模式,并且会将当前活动的类名作为文件名

       如:

SharedPreferences spf = getPreferences(MODE_PRIVATE);

 

      3).使用PreferenceManager类中的 getDefaultSharedPreferences() 方法,它接收一个Context参数,并且用包名作为前缀来命名文件

       如:

SharedPreferences spf = PreferenceManager.getDefaultSharedPreferences(this);

 

  2.再得到SharedPreferences.Editor对象:

      使用SharedPreferences对象的 edit() 方法。

SharedPreference.Editor editor = spf.edit();

 

  3.开始添加数据:

      以键值对的方式写入数据。

editor.putInt("age",22);
editor.putString("name","Visen");
editor.putBoolean("singleDog",false)

 

  4.提交操作:

editor.commit();

 

从SharedPerferences中读取数据:

  提供了一系列的get方法进行数据的读取。如:

String name = editor.getString("name"," ");

 

  如果键所对应的值不存在,则填入设定的默认值。

 

 

简单的保存密码功能

login.xml 登录布局页面

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:stretchColumns="1">


    <LinearLayout
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="240dp"
            android:src="@drawable/image1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/title"
            android:textSize="40sp"
            android:textColor="@color/red"
            android:gravity="center"
            android:background="@color/cyan"/>
    </LinearLayout>

   <TableRow
       android:layout_marginTop="30dp">
       <TextView
           android:layout_height="wrap_content"
           android:text="@string/account"
           android:textSize="30sp"
           android:textColor="@color/white"/>
       <EditText
           android:id="@+id/account"
           android:layout_height="wrap_content"
           android:inputType="text"
           android:textSize="20sp"
           android:textColor="@color/red"
           android:gravity="center"
           android:singleLine="true"/>
   </TableRow>

    <TableRow>
        <TextView
            android:layout_height="wrap_content"
            android:text="@string/password"
            android:textSize="30sp"
            android:textColor="@color/white"/>
        <EditText
            android:
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Android安全开发之浅谈密钥硬编码 下一篇Eclipse调试Android App若选择“U..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目