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 ...
随机推荐
- 可用版本的host
http://blog.csdn.net/ljphhj/article/details/11939591 http://my.oschina.net/lvkun0223/blog/282356 两者的 ...
- css设置水平垂直居中
关于CSS设置水平以及垂直居中的解决方案 想要水平居中? 内联的元素(文字)? .center-children { text-align: center;} 块级元素? .center-me { m ...
- 有意思的数学题:Trapping Rain Water
LeetCode传送门 https://leetcode.com/problems/trapping-rain-water/ 目标:找出积木能容纳的水的“面积”,如图中黑色部分是积木,蓝色为可容纳水的 ...
- oschina大数据开源软件
Hadoop 图形化用户界面 Hue 大数据可视化工具 Nanocubes 企业大数据平台 RedHadoop 大数据查询引擎 PrestoDB Hadoop集群监控工具 HTools 安全大数据分析 ...
- COJ 0138 NOIP201108计算系数
NOIP201108计算系数 难度级别:A: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给定一个多项式(ax + by)^k,请求出多项式 ...
- 删除WIN7系统的共享文件
运行里面输入fsmgmt.msc命令,点开共享目录,选定要取消共享的文件右键,停止共享.
- 【Linux】部署apache
部署前的准备: 1.安装apr #wget http://mirror.bit.edu.cn/apache/apr/apr-1.4.8.tar.gz #tar -zxvf apr-1.4.8.tar. ...
- City Tour
Description Alice想要从城市A出发到城市B,由于Alice最近比较穷(不像集训队陈兴老师是个rich second),所以只能选择做火车从A到B.不过Alice很讨厌坐火车,火车上人比 ...
- java的动态代理设计模式
代码实现: package com.lky.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Me ...
- asdasd
adasdasd asdasd asdasd asd