android开发之路11(用SharedPreferences存储数据)
Android平台给我们提供了一个SharedPreferences类,实际上SharedPreferences处理的就是一个key-value(键值对),它是
一个轻量级的存储类,特别适合用于保存软件配置参数及用户的偏好设置参数,比如登录时候的记住密码功能等。使用
SharedPreferences保存数据,实际上是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下
:
1.获取SharedPreferences对象的两种方式:
①调用Context对象的getSharedPreferences()方法
②调用Activity对象的getPreferences()方法
两种方式的区别:
调用Context对象的getSharedPreferences()方法获得的SharedPreferences对象可以被同一应用程序下的其他组件共享.
调用Activity对象的getPreferences()方法获得的SharedPreferences对象只能在该Activity中使用.
2.SharedPreferences的四种操作模式:
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件
的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入.
3.SharedPreferences类的应用实例:
①创建布局文件:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age" />
<EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numeric="integer"/>
<!--android:onClick用于指定一个方法名称,按钮被点击后就会执行该方法 -->
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:onClick="save"/>
</LinearLayout>
②创建业务类PreferenceService.java
public class PreferenceService {
private Context context;
public PreferenceService(Context context) {
this.context = context;
}
//保存配置参数
public void save(String name,Integer age){
SharedPreferences sp=context.getSharedPreferences("testSP", Context.MODE_PRIVATE);
Editor editor=sp.edit();
editor.putString("name", name);
editor.putInt("age", age);
//将数据提交的文件中
editor.commit();
}
//获取配置参数
public Map<String, String> getPreference(){
//创建Map集合用来保存我们从SharedPreference中获取的数据
Map<String, String> params=new HashMap<String, String>();
SharedPreferences sp=context.getSharedPreferences("testSP", Context.MODE_PRIVATE);
//SharedPreferences类的getString("name", "")方法中第一个参数是参数名,第一个参数是参数的默认值
params.put("name", sp.getString("name", ""));
params.put("age", String.valueOf(sp.getInt("age", 0)));
return params;
}
}
③创建程序的入口MainActivity.java
public class MainActivity extends Activity {
private EditText nameText;
private EditText ageText;
private PreferenceService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText=(EditText) findViewById(R.id.name);
ageText=(EditText) findViewById(R.id.age);
service =new PreferenceService(this);
Map<String, String> params=service.getPreference();
nameText.setText(params.get("name"));
ageText.setText(params.get("age"));
}
/**
* save方法要求:
* 参数必须是View类型
* 且无返回值
*/
public void save(View v){
String name=nameText.getText().toString();
String age=ageText.getText().toString();
service.save(name,Integer.valueOf(age));
Toast.makeText(getApplicationContext(),"保存成功", Toast.LENGTH_LONG).show();
}
}
android开发之路11(用SharedPreferences存储数据)的更多相关文章
- android开发之路09(浅谈SQLite数据库01)
1.SQLite数据库: SQLite 是一个开源的嵌入式关系数据库,实现自包容.零配置.支持事务的SQL数据库引擎. 其特点是高度便携.使 用方便.结构紧凑.高效.可靠. 与其他数据库管理系统不同, ...
- android开发之路12(android四大组件&Fragment&AsyncTask类)
一.Activity组件1.简介:Activity组件是Android四大组件之一,通常一个Activity相当于一个用户界面,我们可以通过加载布局文件将Android提供的各种控件及自定义控件显示到 ...
- android开发之路10(文件的读写)
1.安卓中文件的数据存储实例(将文件保存到手机自带存储空间中): ①MainActivity.java public class MainActivity extends Activity imple ...
- android开发之路03
一.Activity1.如何在一个应用程序中定义多个Activity:①定义一个类,继承Activity:②在该类当中,复写Activity当中的onCreate方法:③在AndroidManifes ...
- android开发之路08(ListView&Adapter)
ListView控件介绍:用于将数据库中的数据或者网络中的数据通过列表的形式显示出来:ListView采用MVC模式将前端显示和后端数据进行分离. 也就是说,ListView控件在装载数据时并不是直接 ...
- android开发之路04(初级android工程师必会,你懂得!)
Android初级Android工程师重点掌握内容如下: 1.Android开发基础: ①UI界面设计: ②SQLite数据库: ③android四大组件: ④android网络编程: ⑤androi ...
- Toast显示图文界面——Android开发之路1
Toast的多种使用方法 Toast其实是一个功能特别强大的组件,不仅仅可以吐司一个文本内容,还可以吐司图片以及图文混排的界面.具体用法如下: 第一种:简单的纯文本内容的吐司: Toast.makeT ...
- 菜单(Menu)的三中创建方式——Android开发之路2
菜单的三种创建方式 一.OptionsMenu---选项菜单 Android应用中的菜单默认是隐藏的,只有当用户点击手机上的MENU键,系统才会显示菜单.这种菜单叫做选项菜单(Options Menu ...
- Android中隐藏顶部状态栏的那些坑——Android开发之路3
Android中隐藏顶部状态栏的那些坑 先看看常规的隐藏状态栏的方法: 方法一: @Override protected void onCreate(Bundle savedInstanceState ...
随机推荐
- [Hive - LanguageManual] Archiving for File Count Reduction
Archiving for File Count Reduction Note: Archiving should be considered an advanced command due to t ...
- 二、python 函数
1.定义函数 def max(x,y): if x>y: return x else: return y 如果定义空函数(函数还没想好怎么编写,只是为了让整个代码能够运行起来) def max( ...
- cloud maintenance of OpenNebula
OpenNebula 4.4.1 maintenance release,官方建议当前的生产环境使用3.x or 4.x的其它版本; php调用curl工具伪造ip Upgrading from Op ...
- Nginx和Tengine的详细安装图文教程(Linux下)
简洁安装 安装依赖 yum -y install gcc openssl-devel pcre-devel zlib-devel 编译三步走./configure \ --prefix=/opt/sx ...
- 提高iOS开发效率的方法和工具
http://www.cocoachina.com/ios/20150717/12626.html 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先 ...
- BAT-使用BAT生成快捷方式
@( echo [InternetShortcut] echo URL=C:\Windows\System32\calc.exe echo IconIndex=0 echo IconFile=C:\W ...
- Delphi异形窗口之PNG
//1.单元内容 unit UnitAlienForm; interface uses Windows, Forms, Classes, Graphics; //从文件加载PNG procedure ...
- mysql查询数据库大小和表
每个mysql都有一个库information_schema,里面有一张表TABLES存储了所有数据库表的信息,因此,可以从这张表中查看数据库大小和表大小 查询数据库大小 ,),'GB') as da ...
- 四轴飞行diy全套入门教程(从最基础的开始)
转载:http://www.cnmox.com/thread-12460-1-1.html首先声明本人也是菜鸟,此教程就是从一个菜鸟的角度来讲解,现在论坛上的帖子都突然冒很多名词出来,又不成体系,我自 ...
- redis的使用
phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/ow ...