[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前三种数据操作方式都只是针对本应用内数据,程序 ...
随机推荐
- 【java】浅谈for循环
for语法: for(初始化条件; 判断条件(bool型,不可缺省); 条件改变)// 初始化条件,条件改变可以是多条,eg for(x=1,y=1;x<4;x++,y++) { 执行的操作 } ...
- 用DLL实现插件的简单演示
这是DLL的代码 library MyDll; uses SysUtils, Dialogs, Classes; procedure ShowInfo(info:PChar);stdcall; beg ...
- linux 服务器之间配置免密登录
客户机:172.16.1.2 远程机:172.16.1.3 1.远程机 a.允许root用户通过22端口登录 vi /etc/ssh/sshd_config PORT 22 PermitRootLog ...
- variable 'QJsonArray array' has initializer but incomplete type
variable "xxx" has initializer but incomplete type 编译报以上错误 分析:“xxx”对应的类型没有找到,没包含定义该变量类型的头文 ...
- 黄聪:jquery+Datatables出现数据过长,表格不自动换行,columns设置width失效的办法
添加下面的CSS代码即可: table.dataTable.nowrap th, table.dataTable.nowrap td{white-space: normal !important;}
- react事件中的this指向
在react中绑定事件处理函数的this指向一共有三种方法,本次主要总结这三种方式. 项目创建 关于项目的创建方法,在之前的文章中有记录,这里不再赘述,项目创建成功后,按照之前的目录结构对生成的项目进 ...
- BIO & NIO & NIO常见框架
BIO & NIO BIO - Blocking IO - 同步式阻塞式IO --- UDP/TCP NIO - New IO - 同步式非阻塞式IO AIO - Asynchronous ...
- Rabbitmq 安装&启动
安装socat [root@Aliyun software]# yum -y install epel-release [root@Aliyun software]# yum -y install s ...
- 高精度算r的n次方 问题 H: 乾隆巡江南
问题 H: 乾隆巡江南 时间限制: 2 Sec 内存限制: 128 MB提交: 13 解决: 3[提交][状态][讨论版] 题目描述 话说乾隆带着他的宰相刘罗锅和你出巡江南,被杭州城府邀请去听戏, ...
- HTML5绘制饼图示例(一)
原文地址:http://www.2cto.com/kf/201108/100251.html HTML5引入Canvas元素,用于图形的绘制,我们可以仅仅基于HTML和JavaScript就能绘制出原 ...