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. 让ASP.NET第一次请求不变慢

    网页开发者早期最常遇到一个问题,当网站布署到IIS时候,每当IIS或是Application Pool重启后,第一位进入网站的使用者,其体验往往第一个反应就是网站很慢,然后就一直询问IT为什么网站这么 ...

  2. Android学习笔记之Toast详解

    1. 贴一段Android API-Toast Toast public class Toast  extends Object java.lang.Object ↳ android.widget.T ...

  3. tableau-基本函数

    一.数据术语 维度——包含诸如文本和日期等类别数据的字段. 度量——包含可以聚合的数字的字段. 二.字段图标 Abc  蓝色图标->离散字段 #     绿色图标->连续字段 =Abc = ...

  4. (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切

    if (this == null) Console.WriteLine("this is null"); 这句话一写,大家一定觉得荒谬,然而 if 内代码的执行却是可能的!本文讲介 ...

  5. 网站使用QQ登录问题小结

    关于网站如何使用QQ登陆这个问题就不多说了,很简单,登陆connect.qq.com找到相应的SDK,下载下来,里面会有demo,将相应的appid,appkey和回调地址callback改成自己的就 ...

  6. matlab中卷积编码参数的理解

    poly2trellis(7, [171 133])代表什么意思呢?首先是7,他是1*k的vector,此处k为1,[171 133]是k*n的vector,此处n就是2,那么这个编码就是1/2码率的 ...

  7. elixir grpc 试用

    备注:  elixir  grpc 封装测试   1.  安装 a. 安装 protoc 参考相关文档,比较简单 b. 安装elixir grpc 插件 protoc-gen-elixir 同时配置环 ...

  8. UML中的几种关系(UML Relationships)

    依赖(Dependency) 依赖可以理解为一个类A使用到了另一个类B,而这种使用关系是具有偶然性的.临时性的.非常弱的,但是B类的变化会影响到A:比如某人要过河,需要借用一条船,此时人与船之间的关系 ...

  9. sublime设置右键在浏览器打开

    用sublime编辑html文件,右键此文件,自带一个在浏览器中打开,但是这个是在IE里打开, 如何在chrome里打开呢. 1. 这里插入一下安装"view in browser" ...

  10. eclipse adt调试出错,不能产出apk问题

    The connection to adb is down, and a severe error has occured http://blog.csdn.net/h7870181/article/ ...