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. windows 10 安装 sql 2005 安装失败

    windows 10 安装 sql 2005 安装失败 网上的方法记录: 安装中无法启动需要先用sp4的补丁文件sqlos.dll,sqlservr.exe 替换D:\Program Files (x ...

  2. 在 Visual Studio中 使用Apache Cordova 开发安卓、iOS程序(自定义图标和闪屏)

    方法1 1台安装有linux的电脑,虚拟机也行,并安装imagemagick软件包.我用的是linux mint,直接在软件管理器中安装即可 把https://github.com/shamork/c ...

  3. Visual->UIElement->FrameworkElement,带来更多功能的同时也带来了更多的限制

    在 WPF 或 UWP 中,我们平时开发所遇到的那些 UI 控件或组件,都直接或间接继承自 Framework.例如:Grid.StackPanel.Canvas.Border.Image.Butto ...

  4. Hadoop 2.2 & HBase 0.96 Maven 依赖总结

    由于Hbase 0.94对Hadoop 2.x的支持不是非常好,故直接添加Hbase 0.94的jar依赖可能会导致问题. 但是直接添加Hbase0.96的依赖,由于官方并没有发布Hbase 0.96 ...

  5. P2P UPD打洞原理

    转自:http://blog.pfan.cn/fengfei/18828.html 首先先介绍一些基本概念:            NAT(Network Address             Tr ...

  6. python笔记-10(socket提升、paramiko、线程、进程、协程、同步IO、异步IO)

    一.socket提升 1.熟悉socket.socket()中的省略部分 socket.socket(AF.INET,socket.SOCK_STREAM) 2.send与recv发送大文件时对于黏包 ...

  7. [Aizu2784]Similarity of Subtrees

    vjudge Description 给一棵\(n\)个节点的有根树,定义两棵树同构当且仅当他们每个深度的节点个数相同.问这个树上有多少对子树满足同构.\(n\le100000\). sol 树\(h ...

  8. java中static关键字的理解(转载)

    static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键字的用法和平常容易误解的地方,最后列 ...

  9. redis+php微博功能的redis数据结构设计总结(四)

    概述: 1.完全采用redis作为数据库实现微博的登录2.发布3.微博的显示4.实现整个功能使用了redis的string,list,hashes四个数据类型,以及string类型的数值自增功能 一. ...

  10. Python nltk English Detection

    http://blog.alejandronolla.com/2013/05/15/detecting-text-language-with-python-and-nltk/ >>> ...