转载请标注:转载于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. Android 项目中的资源获取方法

    Android资源文件分类: Android资源文件大致可以分为两种: 第一种是res目录下存放的可编译的资源文件: 这种资源文件系统会在R.java里面自动生成该资源文件的ID,所以访问这种资源文件 ...

  2. C#中全局处理异常方式

    using System; using System.Configuration; using System.Text; using System.Windows.Forms; using ZB.Qu ...

  3. IntelliJ IDEA Configuring projects

    https://www.jetbrains.com/help/idea/configuring-projects.html Configuring projects A project in Inte ...

  4. sklearn.cross_validation 0.18版本废弃警告及解决方法

    转载:cheneyshark 机器环境: scikit-learn==0.19.1 Python 2.7.13 train_test_split基本用法 在机器学习中,我们通常将原始数据按照比例分割为 ...

  5. vc++获取网页源码之使用类型库(TypeLib)生成包装类

    1.在MFC项目名称上 右击->添加->选择Visual C++下的MFC->TypeLib中的MFC类->添加 可以从注册表表中共或是文件中根据相应的接口生成对应的包装类 效 ...

  6. Web jsp开发学习——Servlet提交表单时用法

     实现提交表单以后判断输入的信息是否符合条件    若符合条件   先新建servlet  Sevlet的两种声明方式,二选一即可  再到web.xml里注册   register.jsp就是表单的界 ...

  7. 在docker宿主机上查找指定容器内运行的所有进程的PID

    转载 https://www.cnblogs.com/keithtt/p/7591097.html 找到指定容器的所有进程的PID可以更方便的对容器进程进行管理,特别是在某些容器卡住无法连接的场景. ...

  8. Socket拆包和解包

    对于基于TCP开发的通讯程序,有个很重要的问题需要解决,就是封包和拆包.下面就针对这个问题谈谈我的想法,抛砖引玉.若有不对,不妥之处,恳求大家指正.在此先谢过大家了. 一.为什么基于TCP的通讯程序需 ...

  9. PyCharm的模板设置

    在File—settings—Editor—File and Code Templates—Python script 脚本里添加 编辑内容 (a)shebang行 #!/usr/bin/python ...

  10. SSH框架总结(环境搭建+框架分析+实例源码下载)

    一.SSH框架简介 SSH是struts+spring+hibernate集成的web应用程序开源框架. Struts:用来控制的,核心控制器是Controller. Spring:对Struts和H ...