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 ...
随机推荐
- android项目中各个文件的介绍
src:java源码gen:自动生成 R.javaandroid.jarandroid Dependenvies 支持jar包assets:资产目录 小的数据库 网页 bin:编译生成的临时文件lib ...
- 【HDOJ】1500 Chopsticks
DP. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm ...
- 用DIV+Css+Jquery 实现的旧版微信飞机大战。
用jquery 实现的旧版微信飞机大战. 以前一直都是做后台和业务逻辑,前端很少去做, 现在个小游戏. 方向键控制方向,Ctrl 键 放炸弹(当然你的有炸弹,哈哈)! 主要都是用div+Css实现的, ...
- POJ_3083——贴左右墙DFS,最短路径BFS
Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and mus ...
- FZU 11月月赛D题:双向搜索+二分
/* 双向搜索感觉是个不错的技巧啊 */ 题目大意: 有n的物品(n<=30),平均(两个人得到的物品差不能大于1)分给两个人,每个物品在每个人心目中的价值分别为(vi,wi) 问两人心目中的价 ...
- StoryBoard 设置TabBar SelectImage 和tintColor
如图:StoryBoard 结构是 Tabbar + Navi + ViewController 需求:需要修改TabBar的Image 和SelectImage 设置Image 设置SelectIm ...
- JavaScript 随机数函数
Math.random()*(m-n)+n random函数语法 Math.random(); random函数返回值 返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) 返回10-20 ...
- c++应用程序文件的编译过程
这里讲下C++文件的编译过程及其中模板的编译过程: 一:一般的C++应用程序的编译过程. 一般说来,C++应用程序的编译过程分为三个阶段.模板也是一样的. 在cpp文件中展开include文件 ...
- Java基础知识强化37:StringBuffer类之StringBuffer的构造方法
1. StringBuffer的构造方法: (1)StringBuffer(): (2)StringBuffer(CharSequence seq): (3)StringBuffer(int capa ...
- Java初转型-MavenWEB项目搭建
http://www.cnblogs.com/xdp-gacl/p/4054814.html