Android中SharePreferences的简单实现
Android中提供SharePreferences这种轻量级的数据存储模式,这种模式能够存储少量数据,并能为自身和其他应用提供数据接口。相对于其他数据存储方式,SharePreferences更加轻量。以下是整个SharePreferences实现的代码:
xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText" android:width="200dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
android:id="@+id/button"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示"
android:id="@+id/button2"/>
</LinearLayout>
java主文件:
package com.example.sharepreferencesTest; import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
private EditText user;
private EditText address;
private Button login;
private Button show;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); user = (EditText)findViewById(R.id.editText);
address = (EditText)findViewById(R.id.editText2);
login = (Button)findViewById(R.id.button); login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//写入SharedPreferences中
SharedPreferences user_ino = getSharedPreferences("user_ino", Context.MODE_PRIVATE); //使用MODE_PRIVATE模式
SharedPreferences.Editor editor = user_ino.edit(); String ename = user.getText().toString();
String eaddress = address.getText().toString(); editor.putString("name",ename);
editor.putString("address", eaddress);
editor.commit();
}
}); show = (Button)findViewById(R.id.button2);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//读入SharedPreferences中的值
SharedPreferences user_ino2 = getSharedPreferences("user_ino", Context.MODE_PRIVATE);
String name1 = user_ino2.getString("name","");
String age1 = user_ino2.getString("address",""); //显示出读出的值
TextView tv1 = (TextView)findViewById(R.id.textView);
TextView tv2 = (TextView)findViewById(R.id.textView2);
tv1.setText(name1);
tv2.setText(age1);
}
}); }
}
Android中SharePreferences的简单实现的更多相关文章
- [原创]Android中LocationManager的简单使用,获取当前位置
Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...
- android中的回调简单认识
首先说一下最抽象的形式--2个类,A类和B类.A类含有1个接口.1个接口变量.(可能含有)1个为接口变量赋值的方法以及1个会使用接口变量的"地方";B类实现A中的接口,(可能)含有 ...
- Android中ProgressDialog的简单示例
网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下: 建立android工程等工作都略去,Google一下就可以了. 下面来介绍主 ...
- iOS 和Android中的正则表达式简单使用
ios 中需要使用NSRegularExpression类,NSTextCheckingResult类. 下面给出最基本的实现代码 NSRegularExpression *regex = [NSRe ...
- Android中实现一个简单的逐帧动画(附代码下载)
场景 Android中的逐帧动画,就是由连续的一张张照片组成的动画. 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 ...
- Android中Tomcat的简单配置和使用
因为学Android已经有一段时间了,但是在学校,服务器方面是个短板啊,没有专门的服务器拿给我们学生练手,所以只有自己找办法了.当然,Tomcat就是不二的选择了. 在网上看了看资料,还是觉得自己记录 ...
- Android中SharedPerforences的简单使用示例 --Android基础
SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedP ...
- Android笔记(二十八) Android中图片之简单图片使用
用户界面很大程度上决定了APP是否被用户接收,为了提供友好的界面,就需要在应用中使用图片了,Android提供了丰富的图片处理功能. 简单使用图片 使用Drawable对象 为Android应用增加了 ...
- Android中ContentProvider的简单使用
1.新建继承ContentProvider的类 package com.wangzhu.demo; import android.content.ContentProvider; import and ...
随机推荐
- [Linux] 输出文件的指定行
1.获取第k行(以k=10为例) 要注意的是,如果文件包含内容不足10行,应该不输出. # Read from the file file.txt and output the tenth line ...
- P4语言编程详解
1.源码目录结构 P4项目源码可以在github上直接获取(https://github.com/p4lang).P4项目由很多个单独的模块组成,每个模块就是一个子项目,下面分别简单介绍一下各模块的功 ...
- JavaScript HTML Handlebars Template
/********************************************************************* * JavaScript HTML Handlebars ...
- 推荐sinaapp谷歌搜索引擎,firefox自定义搜索引擎
满园春色关不住,从此不用再FQ. 一直都在用谷歌,也一直都被和谐. 直到在迅影实习,知道了这个网站:http://goog.sinaapp.com/ 下面说说在firefox里面添加搜索引擎 首先打开 ...
- hiho1601最大分数 DP
#1601 : 最大得分 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在玩一个游戏.给定一个数组A=[A1, A2, ... AN],小Hi可以指定M个 ...
- LOJ2422 NOIP2015 斗地主 【搜索+贪心】*
LOJ2422 NOIP2015 斗地主 LINK 题目大意很简单,就是问你斗地主的一分手牌最少多少次出完 然后我们发现对于一种手牌状态,不考虑顺子的情况是可以贪心做掉的 然后我们直接枚举一下顺子出牌 ...
- Java 层级的简单理解
在J2EE项目中,开发的都是分层来做的: 1.service层:用于暴露给网络调用 2.Impl层:统一规范接口 3.bean层:实体对象,也就是表 4.DAO(Data Access Object) ...
- 《selenium2 python 自动化测试实战》(14)——下载文件
说下载文件之前,我再和大家说一下用cookie登录的事,既然我们用cookie登录,那么传过去的cookie肯定是要和对应的网站完全一致的,注意,是包括大小写哦,本来我也是不知道,我用这个方法登录自己 ...
- Sublime Text 3常用插件安装
Sublime Text 3常用插件安装 PS:sublime是笔者用过的最好用的编辑器,也是最轻量级,功能最强大的编辑器.好东西应该被分享! 1.直接安装 --下载安装包解压缩到Packages目录 ...
- 【DUBBO】dubbo的LoadBalance接口
LoadBalance负载均衡, 负责从多个 Invokers中选出具体的一个Invoker用于本次调用,调用过程中包含了负载均衡的算法,调用失败后需要重新选择 --->类注解@SPI说明可以基 ...