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. js之侧边栏分享

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  2. C++头文件预编译与命名空间使用方法

    宏指令的预编译用法,用于多文件的头文件预编译判断 头文件代码: #include <iostream> #ifndef XB_H//预编译判断XB_H代码段是否被执行 #define XB ...

  3. HDU3652 B-number 数位DP第二题

    A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- ...

  4. BZOJ1183 Croatian2008 Umnozak 【数位DP】*

    BZOJ1183 Croatian2008 Umnozak Description 定义一个数的digit-product是它的各个位上的数字的乘积,定义一个数的self-product是它本身乘以它 ...

  5. TLS支持版本检测

    今天有开发同事询问“生产环境的TLS版本是多少”,这个问题还真没太关注过,首先想到的是通过Chrome浏览器的开发者工具查看一下,访问https页面查看如下: 这太概是说使用了TLS1.2,于是截图给 ...

  6. 理解 Roslyn 中的红绿树(Red-Green Trees)

    Roslyn 的 API 是非常易用的.即便如此复杂的 C# 语法,建立的复杂的 C# 语法树,还有其复杂的树遍历和修改过程,也都被其 API 包装得干净简洁. 而这背后是它的重要设计思路 —— 红绿 ...

  7. python笔记-13 mysql与sqlalchemy

    一.RDBMS relational database management system 关系型数据库引入 1.数据库的意义 更有效和合理的存储读取数据的一种方式 关系模型基础上的数据库 -> ...

  8. java编程之POI读取excel表格的内容

    07版本的excel需要另外加一个jar包.xbean.jar的jar包 读取代码模板.利用模板介绍读取excel的一些poi的api这是重点 /** * 读取excel文件 * @Title: re ...

  9. Adreno Profiler 提取手机游戏资源

    https://blog.csdn.net/lly20000/article/details/79774755 step.1 准备工具 1.adb连接工具(我用的cofface adb  ) [cof ...

  10. c#代码加密

    源代码保护:怎样利用MaxtoCode加密dotNet源代码 http://www.webkaka.com/blog/archives/MaxtoCode-encrypt-dotnet-program ...