Android数据篇—SharedPreferences
来源:互联网 发布:淘宝买同款违法吗 编辑:程序博客网 时间:2024/05/26 08:43
Android的四种数据存储方式:
1:SharedPerferences;
2:SQLite;
3:Content provider;
4:File。
今天主要讨论的是第一种数据存储SharedPreferences;
SharedPreferences:
1:是一种轻型的数据存储方式;
2:本质是基于XML文件存储key-value键值对数据;
3:通常用来存放一些简单的配置信息(是否保存密码)。
如何使用SharedPreferences呢?
1:SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。
2:实现SharedPreferences存储的步骤如下:
(1) 获得SharedPreferences对象;
(2) 获得SharedPreferences.Editor对象;
(3) 通过Editor接口的putXxx方法保存key-value对其中Xxx表示不同的数据类型。
下面我们通过使用SharedPreferences实现一个简单的用户登录功能:
activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.cson.csdnway.MainActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="16dp" android:text="用户名:" /> <EditText android:id="@+id/etuserName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/textView1" android:layout_toRightOf="@+id/textView1" android:ems="10" /> <TextView android:id="@+id/aa" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/etuserName" android:text="密码" /> <EditText android:id="@+id/etuserpass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etuserName" android:layout_alignParentRight="true" android:layout_alignTop="@+id/aa" android:ems="10" > <requestFocus /> </EditText> <CheckBox android:id="@+id/chkSaveName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:checked="false" android:layout_below="@+id/etuserpass" android:text="保存用户名" /> <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/chkSaveName" android:onClick="doClick" android:text="登陆" /> <Button android:id="@+id/btnCancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/btnLogin" android:layout_alignBottom="@+id/btnLogin" android:layout_toRightOf="@+id/btnLogin" android:onClick="doClick" android:text="取消" /></RelativeLayout>MainActivity.java
package com.cson.csdnway;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends AppCompatActivity { private EditText etUserName,etUserPass; private CheckBox chk; private SharedPreferences pref; private SharedPreferences.Editor edtior; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etUserName = (EditText) findViewById(R.id.etuserName); etUserPass = (EditText) findViewById(R.id.etuserpass); chk = (CheckBox) findViewById(R.id.chkSaveName); /** * 通过SharedPreferences保存的文件名和权限 */ pref =getSharedPreferences("UserInfo", MODE_PRIVATE); /** * 启用编译器 */ edtior = pref.edit(); String name = pref.getString("userName", ""); /** * 通过是否勾选复选框来决定是否显示用户名 */ if (name==null) { chk.setChecked(false); }else { chk.setChecked(true); etUserName.setText(name); } } public void doClick(View v){ switch (v.getId()) { case R.id.btnLogin: /** * trim():删除字符串首尾的空白 */ String name = etUserName.getText().toString().trim(); String pass = etUserPass.getText().toString().trim(); /** * 判断获取的值与指定的值是否相同 */ if ("admin".equals(name)&&"123456".equals(pass)) { if (chk.isChecked()) { /** * 复选框勾选的情况下将用户名通过SharedPreferences保存 */ edtior.putString("userName", name); edtior.commit(); }else { edtior.remove("userName"); edtior.commit(); } Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show(); }else { Toast.makeText(MainActivity.this, "禁止登陆", Toast.LENGTH_LONG).show(); } break; default: break; } }}
1 0
- Android数据篇—SharedPreferences
- Android数据储存——SharedPreferences储存
- Android数据保存——SharedPreferences
- Android——SharedPreferences数据存储
- Android——数据存储之SharedPreferences
- Android数据储存——SharedPreferences储存
- Android数据存储——SharedPreferences
- Android存储数据方法——SharedPreferences
- android 数据存取——SharedPreferences
- Android数据存储——SharedPreferences存储
- Android数据存储 —— SharedPreferences
- Android——SharedPreferences存储数据方式
- Android数据存储——SharedPreferences
- Android数据存储之——SharedPreferences
- Android数据持久化——SharedPreferences
- 【笔记】Android数据存储——SharedPreferences
- Android数据存储-SharedPreferences
- Android 保存数据SharedPreferences
- 医院大数据解决方案
- 准备加入软件开发行业的朋友可以看看
- 百度地图
- 小话设计模式(六)适配器模式
- AR或许会带来新的网络隐患 最明智的是未雨绸缪
- Android数据篇—SharedPreferences
- XListView下拉刷新上拉加载
- 第26周-window程序设计(基础篇)-第5章(图形基础)-取得设备内容的多方法
- 二叉树的镜像
- Fragment+ViewPager+Button
- XML解析之pull
- 微信小程序把玩(十六)form组件
- jquery多选获取其选中的值
- 代码里写控件的一些例子