SharePreferences用法
2015-11-17 16:35
260 查看
开发应用需要保存一些配置参数,对于Android应用来说,我们最适合采用SharedPreferences保存数据,它是一个轻量级的存储类,特别适合用于保存软件配置参数。
getSharedPreferences(name,mode)方法的第一个参数用于指定该文件的名称,名称不用带后缀,后缀会由Android自动加上。方法的第二个参数指定文件的操作模式,共有四种操作模式,分别是:
Context.MODE_PRIVATE = 0
Context.MODE_APPEND = 32768
Context.MODE_WORLD_READABLE = 1
Context.MODE_WORLD_WRITEABLE = 2
解析:
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
Context.MODE_WORLD_READABLEContext.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。`
.Java文件
结果
如何使用SharePreferences保存数据
使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data//shared_prefs目录下。例如:SharedPreferences sharedPreferences =getSharedPreferences("mltest", Context.MODE_PRIVATE); Editor editor = sharedPreferences.edit();//获取编辑器 editor.putString("name", "四种模式"); editor.putInt("age", 4); editor.commit();//提交修改
getSharedPreferences(name,mode)方法的第一个参数用于指定该文件的名称,名称不用带后缀,后缀会由Android自动加上。方法的第二个参数指定文件的操作模式,共有四种操作模式,分别是:
Context.MODE_PRIVATE = 0
Context.MODE_APPEND = 32768
Context.MODE_WORLD_READABLE = 1
Context.MODE_WORLD_WRITEABLE = 2
解析:
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
Context.MODE_WORLD_READABLEContext.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。`
如何使用SharePreferences读取数据
SharedPreferences sharedPreferences = getSharedPreferences("ljq", Context.MODE_PRIVATE); //getString()第二个参数为缺省值,如果preference中不存在该key,将返回缺省值 String name = sharedPreferences.getString("name", ""); int age = sharedPreferences.getInt("age", 1);
举例
main.xml布局文件<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" android:textSize="20px" android:id="@+id/nameLable" /> <EditText android:layout_width="80px" android:layout_height="wrap_content" android:layout_toRightOf="@id/nameLable" android:layout_alignTop="@id/nameLable" android:layout_marginLeft="10px" android:id="@+id/name" /> </RelativeLayout> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20px" android:text="@string/age" android:id="@+id/ageLable" /> <EditText android:layout_width="80px" android:layout_height="wrap_content" android:layout_toRightOf="@id/ageLable" android:layout_alignTop="@id/ageLable" android:layout_marginLeft="10px" android:id="@+id/age" /> </RelativeLayout> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/showButton" android:layout_toRightOf="@id/button" android:layout_alignTop="@id/button" android:id="@+id/showButton" /> </RelativeLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20px" android:id="@+id/showText" /> </LinearLayout>
.Java文件
package com.ljq.activity; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class SpActivity extends Activity { private EditText nameText; private EditText ageText; private TextView resultText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); nameText = (EditText)this.findViewById(R.id.name); ageText = (EditText)this.findViewById(R.id.age); resultText = (TextView)this.findViewById(R.id.showText); Button button = (Button)this.findViewById(R.id.button); Button showButton = (Button)this.findViewById(R.id.showButton); button.setOnClickListener(listener); showButton.setOnClickListener(listener); // 回显 SharedPreferences sharedPreferences=getSharedPreferences("ljq123", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE); String nameValue = sharedPreferences.getString("name", ""); int ageValue = sharedPreferences.getInt("age", 1); nameText.setText(nameValue); ageText.setText(String.valueOf(ageValue)); } private View.OnClickListener listener = new View.OnClickListener(){ public void onClick(View v) { Button button = (Button)v; //ljq123文件存放在/data/data/<package name>/shared_prefs目录下 SharedPreferences sharedPreferences=getSharedPreferences("ljq123", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE); switch (button.getId()) { case R.id.button: String name = nameText.getText().toString(); int age = Integer.parseInt(ageText.getText().toString()); Editor editor = sharedPreferences.edit(); //获取编辑器 editor.putString("name", name); editor.putInt("age", age); editor.commit();//提交修改 Toast.makeText(SpActivity.this, "保存成功", Toast.LENGTH_LONG).show(); break; case R.id.showButton: String nameValue = sharedPreferences.getString("name", ""); int ageValue = sharedPreferences.getInt("age", 1); resultText.setText("姓名:" + nameValue + ",年龄:" + ageValue); break; } } }; }
结果
相关文章推荐
- 学习js所必须要知道的一些
- jquery可定制的在线UEditor编辑器
- js全局变量跟浏览器插件冲突
- jquery实现触发、失去焦点操作并执行ajax操作
- JavaScript基础学习
- UVA 题目825 Walking on the Safe Side(DP)
- JS实现选项卡实例详解
- javascript与Jquery整理
- jQuery $(document).ready()和JavaScript window.onload()事件的区别
- ExtJS4.2学习(18)时间控件(转)
- JS给某一个节点的子节点全部绑定事件
- HTML基础学习 a标签的使用
- 从html5的新特性定位安全问题
- JavaScript Promise 简化javascript
- Html5中的video元素
- JSON.parse()和JSON.stringify()
- css整理
- 前端js过滤表格
- hammerjs jquery的选项使用方法,以给swipe设置threshold和velocity为例
- Jquery操作Cookie