[Android]数据篇 --- SharedPreferences
转载请标注:转载于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的更多相关文章
- wemall app商城源码Android数据的SharedPreferences储存方式
wemall-mobile是基于WeMall的Android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android数据 ...
- 【Android开发日记】之入门篇(七)——Android数据存储(上)
在讲解Android的数据源组件——ContentProvider之前我觉得很有必要先弄清楚Android的数据结构. 数据和程序是应用构成的两个核心要素,数据存储永远是应用开发中最重要的主题之一,也 ...
- Android数据存储方式--SharedPreferences
Android数据存储方式有如下四种:SharedPreferences.存储到文件.SQLite数据库.内容提供者(Content provider).存储到网络服务器. 本文主要介绍一下Share ...
- Android应用开发SharedPreferences存储数据的使用方法
Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...
- Android数据存储之SharedPreferences存储
安卓系统为应用提供了系统级的配置存储方案,它就是靠SharedPreferences接口来实现的,该接口存储的所有信息都是以名值对的形式保存,但其保存的数据类型也仅限于基本数据类型,如字符串.整形.布 ...
- Android数据存储-通过SharedPreferences实现记住密码的操作
在Android中登陆中,为了实现用户的方便,往往需要根据用户的需要进行记住密码的操作,所以,在Android数据存储中SharedPreferences恰恰可以实现这一点 下面,小编将带领大家通过S ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览
Android数据的四种存储方式SharedPreferences.SQLite.Content Provider和File (一) —— 总览 作为一个完成的应用程序,数据存储操作是必不可少的. ...
- Android数据存储(一)----SharedPreferences详解
一.Android数据的存储方式: Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File:此外还有一种网络存储 ...
- Android数据存储之sharedpreferences与Content Provider
android中对数据操作包含有: file, sqlite3, Preferences, ContectResolver与ContentProvider前三种数据操作方式都只是针对本应用内数据,程序 ...
随机推荐
- 开源Astro(SparkSQL On HBase)
华为2015年7月20日在O'Reilly Open Source Convention (OSCON) 上宣布Spark SQL on HBase package正式开源.Spark SQL on ...
- WPF Demo16 资源
<Window x:Class="RescourceDemo1.MainWindow" xmlns="http://schemas.microsoft.com/wi ...
- Flask-状态保持-CSRF
问题:cookies基于浏览器的同源策略,不同域名的cookie不能相互访问,为什么可以进行跨站请求伪造呢? 原因:cookie基于浏览器的同源策略,确实是在实现状态保持的时候,不能跨域访问. 跨站请 ...
- vagrant box保存路径修改
add box的时候默认保存在C盘用户文件夹 C:\Users\xxx.vagrant.d,通过设置VAGRANT_HOME环境变量改变默认位置 WIN setx VAGRANT_HOME “X:/y ...
- PAT 乙级 1042 字符统计(20) C++版
1042. 字符统计(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 请编写程序,找出一段给定文字中出现最 ...
- 杂项:TMT(数字媒体产业)
ylbtech-杂项:TMT(数字媒体产业) TMT(Technology,Media,Telecom),是科技.媒体和通信三个英文单词的缩写的第一个字头,整合在一起.含义实际是未来(互联网)科技.媒 ...
- SecureCRT & SecureFx 绿色破解版
租了腾讯云的服务器,是 ubuntu 版的,需要用到 SecureCRT 工具来远程链接,但是连接的只是控制台,只能输入命令操作,如果要下载文件什么的,就很麻烦,这时可以使用 SecureFx 来传输 ...
- 廖雪峰Java3异常处理-2断言和日志-4使用Log4j
1.Log4j Log4j是目前最流行的日志框架.有两个版本 1.x:Log4j 2.x:Log4j2 Log4j下载地址https://www.apache.org/dyn/closer.lua/l ...
- USB-IF协会公布最新PD3.0(PPS)协议认证芯片和产品名单
原文: http://www.chongdiantou.com/wp/archives/25510.html 2017年的骁龙技术峰会高通带来了第一款兼容USB PD3.0(PPS)的QC4+充电器, ...
- React 懒加载组件
//组件第一次初始化的时候加载. import React, {PropTypes} from 'react'; //import AppComposer from './views/App/AppC ...