SharedPreferences是使用键值对的方式来存储数据。

要想使用SharedPreferences来存储数据,必须获取SharedPreferences对象,获取SharedPreferences对象的方法。


Context.getSharedPreferences( )


Activity.getPreferences( )


PreferenceManager.getDefaultSharedPreferences( )


方式:

1、获取SharedPreferences对象

2、通过SharedPreferences对象的edit( )方法获取SharedPreferences.Editor对象

3、向SharedPreferences.Editor对象中添加数据

4、使用commit( )方法提交数据


package com.example.sharedprferencestest;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button save_btn = (Button)findViewById(R.id.save_button);
save_btn.setOnClickListener(this);
} @Override
public void onClick(View v) { SharedPreferences sharedPreferences = getSharedPreferences("data2",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("用户名","wz");
editor.putString("密码","root");
editor.putBoolean("是否保存密码",false);
editor.commit();
}
}

package com.example.sharedprferencestest;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button save_btn = (Button)findViewById(R.id.save_button);
save_btn.setOnClickListener(this); Button restore_btn =(Button)findViewById(R.id.restore_button);
restore_btn.setOnClickListener(this);
} @Override
public void onClick(View v) { SharedPreferences sharedPreferences = getSharedPreferences("data2", MODE_PRIVATE);
switch(v.getId()){
case R.id.save_button:
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("用户名","wz");
editor.putString("密码", "root");
editor.putBoolean("是否保存密码", false);
editor.commit();
break;
case R.id.restore_button:
String username = sharedPreferences.getString("用户名",null);
String password = sharedPreferences.getString("密码",null);
boolean hint = sharedPreferences.getBoolean("是否改变密码", false);
Toast.makeText(getApplicationContext(),username,Toast.LENGTH_SHORT).show();
break;
default:
break;
} }
}

记住密码案例
package com.example.account;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
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 implements View.OnClickListener{ private Button btn;
private EditText account;
private EditText password;
private CheckBox rememberPassword; private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); account = (EditText)findViewById(R.id.account);
password = (EditText)findViewById(R.id.password);
btn = (Button)findViewById(R.id.login);
rememberPassword = (CheckBox)findViewById(R.id.remember_password); sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);
editor = sharedPreferences.edit(); boolean isRemember = sharedPreferences.getBoolean("isRemember",false); if(isRemember){
String username = sharedPreferences.getString("account","");
String pwd = sharedPreferences.getString("password","");
account.setText(username);
password.setText(pwd);
rememberPassword.setChecked(true);
} btn.setOnClickListener(this); } @Override
public void onClick(View v) { switch (v.getId()){
case R.id.login:
String accountContent = account.getText().toString();
String passwordContent = password.getText().toString();
if(accountContent.equals("root") && passwordContent.equals("root")){
if(rememberPassword.isChecked()){
editor.putString("account",accountContent);
editor.putString("password",passwordContent);
editor.putBoolean("isRemember",true);
}else{
editor.clear();
}
editor.commit();
Intent intent = new Intent();
intent.setClass(MainActivity.this,LoginSuccess.class);
startActivity(intent);
finish();
}else{
Toast toast = Toast.makeText(MainActivity.this,"account or password is invalid",Toast.LENGTH_SHORT);
toast.show();
}
break;
default:
break;
}
}
}

Android数据持久化技术 — — —SharedPreferences的更多相关文章

  1. Android数据持久化技术 — — —文件存储

    文件保存 package com.example.datastroredtest; import android.app.Activity;import android.os.Bundle;impor ...

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

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

  3. Android数据存储技术

    Android提供了4种数据存储技术,分别是SharedPreferences.Files.SQLite数据库和网络存储数据.(有的开发者认为使用ContentProvider也可以算是一种,但我觉得 ...

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

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

  5. Android数据存储之SharedPreferences存储

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

  6. Android 数据存储之 SharedPreferences储存

    ------------------------------------------SharedPreferences存储--------------------------------------- ...

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

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

  8. Android数据存储三剑客——SharedPreferences、File、SQLite

    Android中常用的数据存储一般有三种方式:SharedPreferences.文件和SQLite数据库,用来保存需要长时间保存的数据.本文将通过几个具体的小实例来讲解这三种方式的具体实现. 数据存 ...

  9. iOS中常用的四种数据持久化技术

    iOS中的数据持久化方式,基本上有以下四种:属性列表 对象归档 SQLite3和Core Data 1.属性列表涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults st ...

随机推荐

  1. appium案例

    import unittest from time import sleep from appium import webdriver import desired_capabilities clas ...

  2. text/plain && text/html

    text/plain和text/html都是Content-Type; text/plain : 页面以文本形式输出 text/html:  页面以html格式输出

  3. SPSS数据分析—广义估计方程

    广义线性模型虽然很大程度上拓展了线性模型的应用范围,但是其还是有一些限制条件的,比如因变量要求独立,如果碰到重复测 量数据这种因变量不独立的情况,广义线性模型就不再适用了,此时我们需要使用的是广义估计 ...

  4. html标签

    HTML常用标签 首先要知道html标签的一些特点: 1.类似“<关键字>”这样由尖括号包关键字组成,例如<html>,<div>…… 2.一般是成对出现的,由开始 ...

  5. ios cordova报gap://ready 弹出框,一直弹

    The iOS app was not working because there was a plugin missing: https://github.com/apache/cordova-pl ...

  6. 为了防止采集,把文章中出现的URL链接随机大小写(PHP实现)

    <?php $string = "http://www.kxblogs.com/n/20161115/74439155.html"; $string = explode('/ ...

  7. onthink 数据库连接配置

    define('UC_DB_DSN', 'mysql://root:@127.0.0.1:3306/app'); // 数据库连接,使用Model方式调用API必须配置此项 /* 数据库配置 */ ' ...

  8. <python 深入理解>变量交换x,y=y,x实现机制--元组

    python中有一种赋值机制即多元赋值,采用这种方式赋值时,等号两边的对象都是元组并且元组的小括号是可选的.通常形式为 x, y, z = 1, 2, 'a string' 等同于 (x, y, z) ...

  9. html/css 浮动练习之井字形布局

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. HEAD FIRST HTML & CSS学习笔记1

    一.指定媒体类型=指定显示设备的类型  P400 有两种方式指定媒体类型: a. 直接在<link>标签中加属性media,例: <link href="print.css ...