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的简单实现的更多相关文章

  1. [原创]Android中LocationManager的简单使用,获取当前位置

    Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...

  2. android中的回调简单认识

    首先说一下最抽象的形式--2个类,A类和B类.A类含有1个接口.1个接口变量.(可能含有)1个为接口变量赋值的方法以及1个会使用接口变量的"地方";B类实现A中的接口,(可能)含有 ...

  3. Android中ProgressDialog的简单示例

    网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下: 建立android工程等工作都略去,Google一下就可以了. 下面来介绍主 ...

  4. iOS 和Android中的正则表达式简单使用

    ios 中需要使用NSRegularExpression类,NSTextCheckingResult类. 下面给出最基本的实现代码 NSRegularExpression *regex = [NSRe ...

  5. Android中实现一个简单的逐帧动画(附代码下载)

    场景 Android中的逐帧动画,就是由连续的一张张照片组成的动画. 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 ...

  6. Android中Tomcat的简单配置和使用

    因为学Android已经有一段时间了,但是在学校,服务器方面是个短板啊,没有专门的服务器拿给我们学生练手,所以只有自己找办法了.当然,Tomcat就是不二的选择了. 在网上看了看资料,还是觉得自己记录 ...

  7. Android中SharedPerforences的简单使用示例 --Android基础

    SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedP ...

  8. Android笔记(二十八) Android中图片之简单图片使用

    用户界面很大程度上决定了APP是否被用户接收,为了提供友好的界面,就需要在应用中使用图片了,Android提供了丰富的图片处理功能. 简单使用图片 使用Drawable对象 为Android应用增加了 ...

  9. Android中ContentProvider的简单使用

    1.新建继承ContentProvider的类 package com.wangzhu.demo; import android.content.ContentProvider; import and ...

随机推荐

  1. arduino 编程基础

    0. setup() 与 loop() 函数 void setup(): // put your setup code here, to run once: 仅执行一次: void loop(): / ...

  2. 跳转后全屏,兼容大部分浏览器JavaScript

    <!DOCTYPE html> <html> <head> <title>测试</title> </head> <body ...

  3. HDU - 5324:Boring Class (CDQ分治&树状数组&最小字典序)

    题意:给定N个组合,每个组合有a和b,现在求最长序列,满足a不升,b不降. 思路:三位偏序,CDQ分治.   但是没想到怎么输出最小字典序,我好菜啊. 最小字典序: 我们倒序CDQ分治,ans[i]表 ...

  4. Codeforces 15E Triangles 【组合计数】

    Codeforces 15E Triangles Last summer Peter was at his granny's in the country, when a wolf attacked ...

  5. iOS 修改通讯录联系人地址(address)崩溃原因分析

    目前项目中需要对iOS系统通讯录进行读取,修改操作.在进行对地址修改的时候,出现了一个奇怪现象: ● 如果contact没有address字段(或者一个全新的contact),对它的address进行 ...

  6. SpringMVC传递数据的流线图

    流程含义解释:(1)HTTP请求到达web服务器,web服务器将其封装成一个httpServletRequest对象(2)springMVC框架截获这个httpServletRequest对象(3)s ...

  7. ffmpeg hls 点播负载均衡简单实现

    备注: 主要是进行文件的切片处理,以及m3u8 的文件前缀添加以达到通过nginx 或者类似的分布式文件工具进行数据切片处理 参考配置如下: ffmpeg -y -i mydemo.mp4 -vcod ...

  8. OpenStack_Swift源代码分析——Ring的rebalance算法源代码具体分析

    1 Command类中的rebalnace方法 在上篇文章中解说了,创建Ring已经为Ring加入设备.在加入设备后须要对Ring进行平衡,平衡 swift-ring-builder object.b ...

  9. oracle之 sqlplus prelim 参数介绍 ( 处理hang )

    从Oracle10g开始,sqlplus提供了一个参数选项-prelim,用这个参数,在系统已经hang的时候.我们可以连接到SGA而不是数据库,也就是说没有session被创建. 一. 通过以下步骤 ...

  10. docker-tomcat

    https://yq.aliyun.com/articles/6894 1,add命令源目录 只能从 ./ 当前目录开始,目标目录是容器的目录,不是服务器的目录 2,server.xml改的东西,里面 ...