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 ...
随机推荐
- uva 10007 Count the Trees
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- ExpandableListView(可展开的列表组件)的说明以及其用法
ExpandableListView的用法和ListView非常像,只是其所显示的列表项应该由ExpandableListAdapter提供,下面是它的xml属性及说明: 然而,接下来是用事实说话了: ...
- zabbix 插件使用问题
[elk@dr-mysql01 frontend]$ ../../bin/logstash -f std02.conf Settings: Default pipeline workers: 8 Pi ...
- cf500C New Year Book Reading
C. New Year Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- HDU-2059龟兔赛跑(基础方程DP-遍历之前的所有状态)
Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成了绝技,能 ...
- 翻译brent ozar的sqlserver dba训练课程——第一章:建立数据库服务器清单
在公司里,走进销售副总裁的办公室,询问他手下有多少销售人员.不,我的意思是你并不要那么做,他们会问你销售工具为什么那么慢. 其实我的意思是,如果你能走进他的办公室问他这个问题.我敢打赌,他会马上回答 ...
- Android 开发 对话框Dialog dismiss和hide方法的区别
http://ningtukun.blog.163.com/blog/static/186541445201310151539697/ dismiss和hide方法都可以隐藏对话框,在需要的时候也可以 ...
- 《Effective C++ 》学习笔记——条款11
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- dojo 学习笔记之dojo.query - query(id) 与query(class)的差别
考虑这个样例:动态创建一个页面的时候,用new listtem()生成多个listitem, 且每一个listitem中都生成一个按钮button. 假设想要给每一个按钮都绑定一个click事件,用d ...
- (第三章)Java内存模型(下)
一.happens-before happens-before是JMM最核心的概念.对于Java程序员来说,理解happens-before是理解JMM的关键. 1.1 JMM的设计 从JMM设计者的 ...