Android学习笔记--存储方案(SharedPreference、文件IO)
1. SharedPreference
SharedPreference可以很容易的保存key-value对,通常用于保存配置信息。
保存的步骤
1. 获得SharedPreferences对象 (最后一个参数指定了文件的建立模式,设置文件属性)
SharedPreferences mySharedPreference = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
2. 获得SharedPreferences.Editor对象
SharedPreferences.Editor editor = mySharedPreference.edit();
3. 保存组件中的值
editor.putString("name",edtname.getText().toString());
editor.putInt("age",Integer.valueOf(edtage.getText().toString()));
4. 提交保存的结果
editor.commit();
demo界面如下,实现的功能:点击保存信息将姓名、年龄保存。重启demo后,点击显示信息,将之前保存的内容显示到界面。
MainActivity.java
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends ActionBarActivity {
//SharedPreference可以很容易的保存key-value对,因此通常用于保存配置信息
//SharedPreference会将key-value对保存在survey.xml文件中
private final String PREFERENCE_NAME = "survey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText edtname = (EditText)findViewById(R.id.edt1);
final EditText edtage = (EditText)findViewById(R.id.edt2);
Button btn1 = (Button)findViewById(R.id.btn1);
Button btn2 = (Button)findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//1. 获得SharedPreferences对象 (最后一个参数指定了文件的建立模式,设置文件属性)
SharedPreferences mySharedPreference = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
//2. 获得SharedPreferences.Editor对象
SharedPreferences.Editor editor = mySharedPreference.edit();
//3. 保存组件中的值
editor.putString("name",edtname.getText().toString());
editor.putInt("age",Integer.valueOf(edtage.getText().toString()));
//4. 提交保存的结果
editor.commit(); Toast.makeText(MainActivity.this, "信息已保存", Toast.LENGTH_SHORT).show();
}
}); btn2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//获得SharedPreferences对象
SharedPreferences mySharedPreference = getSharedPreferences(PREFERENCE_NAME, Activity.MODE_PRIVATE);
//获得保存的值
String name = mySharedPreference.getString("name", "");
int age = mySharedPreference.getInt("age", 0);
Toast.makeText(MainActivity.this, "姓名:"+name+" 年龄:"+age, Toast.LENGTH_SHORT).show(); edtname.setText(name);
edtage.setText(String.valueOf(age));
}
});
} @Override
protected void onStop(){
super.onStop();
}
}
activity_main.xml
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.andtest008sharedpreferences.MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名" />
<EditText
android:id="@+id/edt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint = "请输入姓名"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄" />
<EditText
android:id="@+id/edt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint = "请输入年龄"
/>
<Button
android:id = "@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="保存信息"
/>
<Button
android:id = "@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示信息"
/>
</LinearLayout>
2.文件IO
使用的工具OutputStream/InputStream
向file.txt写入内容
OutputStream outer = openFileOutput("file.txt",Activity.MODE_PRIVATE);
读取file.txt的内容
InputStream inner = openFileInput("file.txt");
package com.evor.test; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView; public class MainActivity extends ActionBarActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); try {
//向文件写入内容
OutputStream outer = openFileOutput("file.txt",Activity.MODE_PRIVATE);
String str = "Test:这是一行测试文字!!";
outer.write(str.getBytes("utf-8"));
outer.close(); //读取文件的内容,并显示在textView
InputStream inner = openFileInput("file.txt");
byte[] buffer = new byte[100];
int bytecount = inner.read(buffer);
String str2 = new String(buffer,0,bytecount,"utf-8");
TextView view = (TextView)findViewById(R.id.showtxt);
view.setText(str2);
inner.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
}
activity_main.xml
<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.evor.test.MainActivity" > <TextView
android:id="@+id/showtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </RelativeLayout>
Android学习笔记--存储方案(SharedPreference、文件IO)的更多相关文章
- Android学习笔记之AndroidManifest.xml文件解析(转)
//自已备注: <?xml version="1.0" encoding="utf-8"?>//说明了版本号,字符集 <manifest xm ...
- Android学习笔记之AndroidManifest.xml文件解析
一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...
- Android学习笔记_43_网络通信之文件断点上传
1.建立服务端,用于接收上传的文件.这里使用Socket,文件可能会比较大.采用多线程编程,防止并发. package com.socket.service; import java.io.File; ...
- Android学习笔记_15_网络通信之文件断点下载
一.断点下载原理: 使用多线程下载文件可以更快完成文件的下载,多线程下载文件之所以快,是因为其抢占的服务器资源多.如:假设服务器同时最多服务100个用户,在服务器中一条线程对应一个用户,100条线程在 ...
- Android学习笔记之AndroidManifest.xml文件解析(详解)
一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...
- Android学习笔记(9):使用XML文件和Java代码控制UI界面
Android推荐使用XML文件设置UI界面.然后用Java代码控制逻辑部分,这体现了MVC思想. MVC全名是Model View Controller.是模型(model)-视图(view)-控制 ...
- Android学习笔记(1)----播放音乐文件
原文地址:http://www.cnblogs.com/wynet/p/5526905.html 这里介绍两种播放资源文件的方法: 第一种. assets类资源放在工程根目录的assets子目录下,它 ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
随机推荐
- CSS浮动属性Float详解
什么是CSS Float? float 是 css 的定位属性.在传统的印刷布局中,文本可以按照需要围绕图片.一般把这种方式称为“文本环绕”.在网页设计中,应用了CSS的float属性的页面元素就像在 ...
- 总结:man, info, hwclock, cal, type, which, dirname, basename, who, which, whatis, makewhatis, hash, tty
man, info, help, date, hwclock, cal, shutdown, reboot, halt, poweroff, type, cd, which, dirname, bas ...
- linux开源论坛
开源资源: 开源http://oss.org.cn/?action-news http://www.lupaworld.com/proj.php http://www.10pig.cn/linux/o ...
- 深入理解linux网络技术内幕读书笔记(六)--PCI层与网络接口卡
Table of Contents 1 本章涉及的数据结构 1.1 pci_device_id结构 1.2 pci_dev结构 1.3 pci_driver结构 2 PCI NIC设备驱动程序的注册 ...
- (转)20 个大大节省你时间的 HTML5 开发工具
Rendera 如果你希望有个环境可以测试.浏览和体验各种不同的 CSS/HTML 和 JavaScript 代码,Rendera 为你提供了实时的运行结果.类似 RunJS. Patternizer ...
- Java finally语句到底是在return之前还是之后执行(JVM字节码分析及内部体系结构)?
之前看了一篇关于"Java finally语句到底是在return之前还是之后执行?"这样的博客,看到兴致处,突然博客里的一个测试用例让我产生了疑惑. 测试用例如下: public ...
- 第12届北师大校赛热身赛第二场 A.不和谐的长难句1
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php? pid=17121 2014-04-25 22:59:49 不和谐的长难句1 Time Limit: ...
- ZOJ 3511 不相交切切多边形 线段树求最大边数
题意: n多凸边形 m刀 (把n切m刀,问切完后的图形中 最多的边数 是多少) 切a点-b点 数据保证切的刀不会相交 思路: 2点之间的剩余点数就是边数, 把a-b距离 近 排序 切完一刀就统计一下切 ...
- Abstract-抽象类
本人理论较差,之前会做却不明原因,最近在改别人的代码发现实现方式完全不同,但对于我这个理论白痴来说完全不知道为什么别人要这么写,好处在哪里. 没有理论的指导,会用也只是不断的Copy前人,永远无法让程 ...
- NHibernate之映射文件配置说明(转载2)
六.鉴别器 在"一棵对象继承树对应一个表"的策略中,<discriminator>元素是必需的, 它定义了表的鉴别器字段. 鉴别器字段包含标志值,用于告知持久化层应 ...