从零開始学android<数据存储(1)SharedPreferences属性文件.三十五.>
在android中有五种保存数据的方法。各自是:
Store data on the web with your own network server.网络存储
今天这一节我们一起来学习Shared
Preferences 属性文件存储的方式来存储简单的数据
我们能够使用Shared
Preferences 来存储 booleans, floats, ints, longs, and strings型的简单数据并以键值对的形式保存为xml文件。
为了实例化Shared
Preferences 我们能够使用
getSharedPreferences()和getPreferences() 这两个方法
第一个方法须要传入一个文件名称和存储的模式
另外一种方法默觉得仅仅有一个属性文件,仅仅须要传入一个存储模式即可了
存储模式 :
MODE_PRIVATE仅本应用可用
MODE_APPEND可追加
MODE_WORLD_READABLE,可被其它应用读
MODE_WORLD_WRITEABLE.可被其它应用写
详细操作见代码凝视
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=".MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="89dp"
android:text="存储信息" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="36dp"
android:text="读取信息" /> </RelativeLayout>
JAVA文件
package com.example.sharedpreferences; import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity {
private SharedPreferences sharedPreferences;
private Button saveData, getDate;
public static final String FILENAME = "flyou"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences(FILENAME, MODE_PRIVATE);
saveData = (Button) this.findViewById(R.id.button1);
getDate = (Button) this.findViewById(R.id.button2);
saveData.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", "jay");
editor.putString("password", "553274238");
Boolean flag = editor.commit();
Toast.makeText(MainActivity.this, "运行完毕,运行结果:-->" + flag, 2)
.show();
}
});
getDate.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
String username = sharedPreferences.getString("username",
"未找到匹配信息");
String password = sharedPreferences.getString("password",
"未找到用户密码");
Toast.makeText(MainActivity.this,
"用户名:——>" + username + ",密码:——>" + password, 2).show();
}
});
}
}
接下来使用改方法来实现本地记住账号和password的功能
里面可能会涉及到一些没有讲到的知识,大家能够先了解下,也对前面学过的其它组件进行一下回想
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=".MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:layout_marginTop="42dp"
android:text="username" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="58dp"
android:text="密 码" /> <EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10" > <requestFocus />
</EditText> <EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:ems="10"
android:inputType="textPassword" /> <CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/editText2"
android:text="记住password" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentLeft="true"
android:text="注冊账号" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/checkBox1"
android:text="登录" /> </RelativeLayout>
登录后界面
<? xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="? android:attr/textAppearanceMedium" /> </LinearLayout>
JAVA文件
package com.example.sharepreferencesdemo; import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText; public class MainActivity extends Activity {
private SharedPreferences sharedPreferences;
private Button login;
private CheckBox checkBox;
private EditText username, password; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) this.findViewById(R.id.button1);
checkBox = (CheckBox) this.findViewById(R.id.checkBox1);
username = (EditText) this.findViewById(R.id.editText1);
password = (EditText) this.findViewById(R.id.editText2);
sharedPreferences = getPreferences(MODE_PRIVATE);// 通过getPreferences实例化sharedPreferences对象 String usernameString = sharedPreferences.getString("username", "");// 读取用户名
username.setText(usernameString);// 为编辑框设置内容 // 获取复选框的选中状态,假设选中的话就 进行记住密码的操作
if (sharedPreferences.getBoolean("checked", false)) {
// 获取密码
String passwordString = sharedPreferences.getString("password", "");
// 设置编辑框信息
password.setText(passwordString);
} login.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 通过edit 方法实例化Editor对象,储存信息
SharedPreferences.Editor editor = sharedPreferences.edit();
// 以键值对的形式储存信息
editor.putString("username", username.getText().toString());
editor.putString("password", password.getText().toString());
// 推断复选框的选中状态并进行存储
if (checkBox.isChecked()) {
editor.putBoolean("checked", true);
} else {
editor.putBoolean("checked", false);
}
// 运行储存操作
editor.commit(); // 设置进度对话框
final ProgressDialog dialog = new ProgressDialog(
MainActivity.this);
// 设置标题
dialog.setTitle("用户登录");
// 设置提示信息
dialog.setMessage("正在登录。请稍后……");
// 開始进度对话框
dialog.onStart();
// 延时线程操作
new Thread() { @Override
public void run() {
// TODO Auto-generated method stub
try {
// 休眠3秒
Thread.sleep(3 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 对话框消失
dialog.dismiss();
// 设置意图转跳
Intent intent = new Intent(MainActivity.this,
main.class); // 传递意图信息
intent.putExtra("username", username.getText()
.toString());
// 開始activity转跳
startActivity(intent);
MainActivity.this.finish();
}
}
}.start();// 開始线程操作
// 显示对话框
dialog.show(); }
}); }
}
登陆后的界面
package com.example.sharepreferencesdemo; import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView; public class main extends Activity {
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
super.setContentView(R.layout.linearlayout);
text=(TextView)this.findViewById(R.id.textView3);
Intent intent=getIntent();
text.setTextSize(15);
text.setGravity(Gravity.CENTER_HORIZONTAL);
text.setTextColor(Color.CYAN);
text.setText("欢迎: "+intent.getStringExtra("username"));
}
}
未点击记住password,第二次登录。
点击记住password登录
点击记住password后。第三次登录
介绍了SharedPreferences属性文件的存储,我们能够进行较小数据的高速存储与便捷读取
下节预报:Internal
Storage内部存储器
从零開始学android<数据存储(1)SharedPreferences属性文件.三十五.>的更多相关文章
- 从零開始学android<TabHost标签组件.二十九.>
TabHost主要特点是能够在一个窗体中显示多组标签栏的内容,在Android系统之中每一个标签栏就称为一个Tab.而包括这多个标签栏的容器就将其称为TabHost.TabHost类的继承结构例如以下 ...
- 第13章、布局Layouts之RelativeLayout相对布局(从零開始学Android)
RelativeLayout相对布局 RelativeLayout是一种相对布局,控件的位置是依照相对位置来计算的,后一个控件在什么位置依赖于前一个控件的基本位置,是布局最经常使用,也是最灵活的一种布 ...
- 从零開始学android<SeekBar滑动组件.二十二.>
拖动条能够由用户自己进行手工的调节,比如:当用户须要调整播放器音量或者是电影的播放进度时都会使用到拖动条,SeekBar类的定义结构例如以下所看到的: java.lang.Object ↳ an ...
- 从零開始学android<mediaplayer自带播放器(视频播放).四十九.>
MediaPlayer除了能够对音频播放之外,也能够对视频进行播放,可是假设要播放视频仅仅依靠MediaPlayer还是不够的.还须要编写一个能够用于视频显示的空间,而这块显示空间要求能够高速的进行G ...
- 从零開始学android<ImageSwitcher图片切换组件.二十六.>
ImageSwitcher组件的主要功能是完毕图片的切换显示,比如用户在进行图片浏览的时候.能够通过button点击一张张的切换显示的图片,并且使用ImageSwitcher组件在每次切换的时候也能够 ...
- 从零開始学android<Menu菜单组件.三十.>
在Android系统之中.菜单一共同拥有三类:选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 今天我们就用几个样例来分别介绍下菜单的使用 acti ...
- 从零開始学android<SlidingDrawer 隐式抽屉.三十三.>
SlidingDrawer是一种抽屉型的组件.当用户选择打开此抽屉之后,会得到一些能够使用的"程序集".这样当一个界面要摆放多个组件的时候,使用此组件就能够非常好的解决布局空间紧张 ...
- 从零開始学android<RelativeLayout相对布局.十六.>
相对布局管理器指的是參考某一其它控件进行摆放,能够通过控制,将组件摆放在一个指定參考组件的上.下.左.右等位置,这些能够直接通过各个组件提供的属性完毕. 以下介绍一下各个方法的基本使用 No. 属性名 ...
- 从零開始学android<使用嵌套布局实现计算器界面.十七.>
所谓的嵌套布局就是在一个文件里嵌套多个布局文件 <span style="font-size:18px;"> <LinearLayout android:layo ...
随机推荐
- POJ 2112 Optimal Milking【网络流+二分+最短路】
求使所有牛都可以被挤牛奶的条件下牛走的最长距离. Floyd求出两两节点之间的最短路,然后二分距离. 构图: 将每一个milking machine与源点连接,边权为最大值m,每个cow与汇点连接,边 ...
- Swift - 自定义单元格实现微信聊天界面
1,下面是一个放微信聊天界面的消息展示列表,实现的功能有: (1)消息可以是文本消息也可以是图片消息 (2)消息背景为气泡状图片,同时消息气泡可根据内容自适应大小 (3)每条消息旁边有头像,在左边表示 ...
- 事件总线帧---Otto
我们如果这样一种业务场景.如今在做一款及时聊天应用,我们在聊天页面进行收发信息.同一时候也要实时更新前一页面的聊天记录,这时我们该怎样去实现?说说我曾经的实现策略.我使用的是广播接收器BroadCas ...
- Java时间比較
Date类有两个方法 一个是after()比方date1.after(date2)推断date1是否在date2之后也就是说date1小于date2吧, 一个是before()比方date1.befo ...
- TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸
之前用Text Kit写Reader的时候,在分页时要计算一段文本的尺寸大小,之前使用了NSString类的sizeWithFont:constrainedToSize:lineBreakMode:方 ...
- VSTO之旅系列(五):创建Outlook解决方案
原文:VSTO之旅系列(五):创建Outlook解决方案 本专题概要 引言 Outlook对象模型 自定义Outlook窗体 小结 一.引言 在上一个专题中,为大家简单介绍了下如何创建Word解决方案 ...
- hdu4280(最大流)
传送门:Island Transport 题意:有N个岛屿 M条无向路 每个路有一最大允许的客流量,求从最西的那个岛屿最多能运用多少乘客到最东的那个岛屿. 分析:无向图正反都加弧,权值一样,这题点多, ...
- linux它SQL声明简明教程---WHERE
我们并不一定必须注意,每次格里面的信息是完全陷入了.在很多情况下,我们需要有选择性地捕捞数据.对于我们的样本.我们可以只抓住一个营业额超过 $1,000 轮廓. 做这个事情,我们就须要用到 WHERE ...
- Tkinter隐藏窗口再让他显示出来的例子
隐藏主要是 : withdraw()函数. 重新显示出来主要是: update()和deiconify()函数. 来源:http://www.blog.pythonlibrary.org/2012/0 ...
- hdu1520(树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1520 题意:举办一个party,候选人当中有很多人之间有上下级关系,求没有直接上下级的最多的人数. 分 ...