我的Android之旅(十一)---数据存储1--SharedPreferences
来源:互联网 发布:手机考勤表软件 编辑:程序博客网 时间:2024/06/08 18:20
SharedPreferences是一种轻量级的数据存储方式,它可以用键值对的方式把简单数据类型(boolean、int、float、long和String)存储在应用程序的私有目录下(data/data/[包名] /shared_prefs/)自己定义的xml文件中。
SharedPreferences是以键值对来存储应用程序的配置信息的一种方式,它只能存储基本数据类型。一个程序的配置文件仅可以在本应用程序中使用,或者说只能在同一个包内使用,不能在不同的包之间使用。实际上sharedPreferences是采用了XML格式将数据存储到设备中,在DDMS 中的File Explorer中的/data/data//shares_prefs下。
下面我们以一个例子来说明如何使用sharedpreferences来存储数据,该例子是在Activity退出时保存界面的基本的信息,当再次运行该程序的时候,就会读取上次保存的信息。
XML文件中的代码:
package com.jerehedu.jereduch10;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;public class SharedPreferencesActivity extends AppCompatActivity {private EditText user,pwd; private Button save; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); user= (EditText) findViewById(R.id.user); pwd= (EditText) findViewById(R.id.pwd); save= (Button) findViewById(R.id.save); final SharedPreferences sp=getSharedPreferences("userInfo", Context.MODE_PRIVATE); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = sp.edit(); editor.putString("userName", user.getText().toString()); editor.putInt("userPwd", Integer.parseInt(pwd.getText().toString())); editor.commit(); Intent intent=new Intent(SharedPreferencesActivity.this, SpIntent2Activity.class); startActivity(intent); } }); }}
布局文件中代码如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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" tools:context=".SharedPreferencesActivity" android:orientation="vertical"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" android:id="@+id/user" android:textSize="20sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:textSize="20sp" android:id="@+id/pwd"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/ll"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" android:id="@+id/c1" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自动登陆" android:id="@+id/c2"/> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/save" android:text="保存" /></LinearLayout>
跳转界面.xml文件中的代码:
package com.jerehedu.jereduch10;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class SpIntent2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sp_intent2); Button getUser= (Button) findViewById(R.id.getUser); final TextView showUser= (TextView) findViewById(R.id.showUser); final SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE); getUser.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name=sp.getString("userName",""); String pwd=String.valueOf(sp.getInt("userPwd",8888)); showUser.setText("用户名:"+name+"\n"+"密码:"+pwd); } }); }}
运行结果如下
点击获取数据的按钮:
若退出界面,重新打开后,数据依然存在。
使用SharedPreferences存取数据
保存key-value对一般要指定一个文件名,然后用类似putString的方法指定key和value。SharedPreferences也采用了同样的方法。使用SharedPreferences保存key-value对的步骤如下:
(1) 使用Activity类的getSharedPreferences方法获得SharedPreferences对象。其中存储key-value的文件名的名称由getSharedPreferences方法的第一个参数指定。
(2) 使用SharedPreferences接口的edit获得SharedPreferences.Editor对象。
(3) 通过SharedPreferences.Editor接口的putXXX方法保存key-value对。其中XXX表示value的不同数据类型。Boolean类型的value则是用putBoolean方法,字符串类型的则为putString方法。
(4) 通过SharedPreferences.Editor接口的commit方法保存key-value对。Commit方法相当于数据库事务中的提交(commit)操作。只有在事件结束后进行提交,才会将数据真正保存在数据库中。保存key-value也是一样。
- 我的Android之旅(十一)---数据存储1--SharedPreferences
- 我的学习之旅:android的数据存储之SharedPreferences
- Android之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
- 二叉排序树
- PAT甲级练习题A1022. Digital Library (30)
- 百度地图-根据起终点经纬度驾车导航
- java中输入几个数字,进行排列
- 双向队列
- 我的Android之旅(十一)---数据存储1--SharedPreferences
- setOnItemClickListener ()和setClickListener ()不同
- atoi函数实现的各种考虑因素
- JZOJ[8.15比赛]题解
- 杭电oj 1232 NYOJ 5198 畅通工程(并查集)
- Centos Git Update
- MongoDB基本操作
- WING
- Python调试与异常