转载请标注:转载于http://www.cnblogs.com/Liuyt-61/p/6637515.html

---------------------------------------------------------------

Android数据的四种存储方式:

      1、SharedPreferences

      2、SQLite

      3、Content Provider

      4、File

----------------------分割线----------------------------------

一、SharedPreferences:

    1.是一种轻型数据存储方式.

    2.本质是基于XML文件存储 key-value 键值对数据

    3.通常用来存储一些简单的配置信息,如用户名、密码(后面附上实例代码)

1>SharedPreferences对象本身只能获取数据而不支持存储和修改

  Editor实现存储和修改

2>实现存储的步骤:

  ①使用Activity类的getSharedPreferences获取其对象,其中存储key-value的文件的名称由getSharedPreferences方法第一个参数指定。

  ②使用SharedPreferences接口的Edit获得SharedPreferences.Editor对象。

  ③通过SharedPreferences.Editor接口的put×××方法保存key-value对

  ④通过SharedPreferences.Editor接口的commit()方法保存key-value对进行提交。

直接上实例代码(登录界面保存用户名)

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名" /> <EditText
android:id="@+id/et_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=""
android:ems=""
android:hint="input username"
android:inputType="textPersonName" >
</EditText>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码" /> <EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=""
android:ems=""
android:hint="input password"
android:inputType="textPassword" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <CheckBox
android:id="@+id/cb_remember"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住用户名" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="登录" /> <Button
android:id="@+id/btn_canel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="取消" />
</LinearLayout> </LinearLayout>

MainActivity.java

package com.Liuyt.s03_e28_sharedpreferences;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity {
private Button btn_login,btn_canel;
private EditText et_username,et_password;
private CheckBox cb_remenber;
private Editor editor; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// SharedPreferences pref = getSharedPreferences("myPref", MODE_PRIVATE);
// Editor editor = pref.edit();
// editor.putString("name", "Liuyt");
// editor.putInt("age",18);
// editor.commit();
// System.out.println(pref.getString("name", ""));
// System.out.println(pref.getInt("age", 0));
btn_canel = (Button) findViewById(R.id.btn_canel);
btn_login = (Button) findViewById(R.id.btn_login);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
cb_remenber = (CheckBox) findViewById(R.id.cb_remember);
SharedPreferences pref = getSharedPreferences("myPref1", MODE_PRIVATE);
editor = pref.edit();
String name = pref.getString("username", "");
if(name == null){
cb_remenber.setChecked(false);
}else{
cb_remenber.setChecked(true);
et_username.setText(name);
}
}
public void doClick(View view){
switch (view.getId()) {
case R.id.btn_login:
String name = et_username.getText().toString().trim();//去掉首尾空格
String password = et_password.getText().toString().trim();
if("admin".equals(name)&&"".equals(password)){
if(cb_remenber.isChecked()){
editor.putString("username", name);
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;
}
} }

[Android]数据篇 --- SharedPreferences的更多相关文章

  1. wemall app商城源码Android数据的SharedPreferences储存方式

    wemall-mobile是基于WeMall的Android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android数据 ...

  2. 【Android开发日记】之入门篇(七)——Android数据存储(上)

    在讲解Android的数据源组件——ContentProvider之前我觉得很有必要先弄清楚Android的数据结构. 数据和程序是应用构成的两个核心要素,数据存储永远是应用开发中最重要的主题之一,也 ...

  3. Android数据存储方式--SharedPreferences

    Android数据存储方式有如下四种:SharedPreferences.存储到文件.SQLite数据库.内容提供者(Content provider).存储到网络服务器. 本文主要介绍一下Share ...

  4. Android应用开发SharedPreferences存储数据的使用方法

    Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...

  5. Android数据存储之SharedPreferences存储

    安卓系统为应用提供了系统级的配置存储方案,它就是靠SharedPreferences接口来实现的,该接口存储的所有信息都是以名值对的形式保存,但其保存的数据类型也仅限于基本数据类型,如字符串.整形.布 ...

  6. Android数据存储-通过SharedPreferences实现记住密码的操作

    在Android中登陆中,为了实现用户的方便,往往需要根据用户的需要进行记住密码的操作,所以,在Android数据存储中SharedPreferences恰恰可以实现这一点 下面,小编将带领大家通过S ...

  7. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览

    Android数据的四种存储方式SharedPreferences.SQLite.Content Provider和File (一) —— 总览   作为一个完成的应用程序,数据存储操作是必不可少的. ...

  8. Android数据存储(一)----SharedPreferences详解

    一.Android数据的存储方式: Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File:此外还有一种网络存储 ...

  9. Android数据存储之sharedpreferences与Content Provider

    android中对数据操作包含有: file, sqlite3, Preferences, ContectResolver与ContentProvider前三种数据操作方式都只是针对本应用内数据,程序 ...

随机推荐

  1. JAVA循环语句

    while循环 求1到5的和 循环输出26个英文字母分两行输出 do while循环 猜拳游戏 这里包含随机数的生成方法Math.random()中数为double[0,1)通过*10和强制类型转换可 ...

  2. Winfrom窗体无法关闭问题--检查是否存在重写

    问题描述: Winfrom窗体无法关闭问题----点击关闭/最大/最小化无法正常相应. 问题来源: 老版本的程序要求使用无边框的Form窗体(实现功能——设置为无边框窗体并重写窗体的关闭.最大.最小化 ...

  3. WPF Demo511 控件共用事件

    路由事件: 1.路由事件一般使用的三种策略如下所示: A.Bubble(冒泡模式):事件从自己激发一直传递到根元素; B.Direct(直接模式):只有事件源才有机会相应事件(和传统事件一样); C. ...

  4. MSSQL 2008 密钥

    sql server2008 r2 密钥 Developer: PTTFM-X467G-P7RH2-3Q6CG-4DMYBEnterprise: JD8Y6-HQG69-P9H84-XDTPG-34M ...

  5. GoJS拖动设计

    http://192.168.0.149:8035/gojs/intro/groups.html http://192.168.0.149:8035/gojs/intro/ports.html htt ...

  6. 详细记录sql运行时间(精确到毫秒)

    写程序的人,往往需要分析所写的SQL语句是否已经优化过了,服务器的响应时间有多快,这个时候就需要用到SQL的STATISTICS状态值来查看了. 通过设置STATISTICS我们可以查看执行SQL时的 ...

  7. C++进阶--静态多态

    //############################################################################ /* * 多态 */ // 常见的动态多态 ...

  8. IDC:机房监控系统

    ylbtech-IDC:机房监控系统 机房监控系统主要是针对机房所有的设备及环境进行集中监控和管理的,其监控对象构成机房的各个子系统:动力系统.环境系统.消防系统.保安系统.网络系统等. 1.返回顶部 ...

  9. lvm的磁盘管理知识点整理

    首先感谢参考的博客网址: http://blog.51cto.com/dreamfire/1084729 https://www.cnblogs.com/kevingrace/p/5825963.ht ...

  10. C语言强化——指针

    目录 相关概念 数组与函数 栈空间和堆空间的差异 指针常量与常量指针 指针数组与数组指针 二级指针 二级指针的传递 二级指针的偏移(索引式排序) 相关概念 指针的大小,在32系统上是4个字节:在64位 ...