activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
 
<EditText
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Please input your username" />
 
<EditText
android:id="@+id/passWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Please input your password"
android:inputType="textPassword" />
 
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:textSize="30sp" />
 
</LinearLayout>

activity_second.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
 
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
 
<Button
android:id="@+id/btn_showinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="showIfo" />
 
</LinearLayout>

MainActivity.java

package com.example.testsharedpreferencesdemo001;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity
{
 
SharedPreferences sharedPreferences;
Editor editor;
EditText userName, passWord;
Button login;
 
@SuppressLint("CommitPrefEdits")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText) findViewById(R.id.userName);
passWord = (EditText) findViewById(R.id.passWord);
 
login = (Button) findViewById(R.id.btn_login);
login.setOnClickListener(myOnClickListener);
 
sharedPreferences = this.getSharedPreferences("login_info",
MODE_PRIVATE);
 
String stored_username = sharedPreferences.getString("username", "");
String stored_password = sharedPreferences.getString("password", "");
userName.setText(stored_username);
passWord.setText(stored_password);
editor = sharedPreferences.edit();
editor.putString("username", userName.getText().toString());
editor.putString("password", passWord.getText().toString());
 
}
 
OnClickListener myOnClickListener = new OnClickListener()
{
 
@Override
public void onClick(View v)
{
String sUserName = userName.getText().toString();
String sPassWord = passWord.getText().toString();
 
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("username", sUserName);
intent.putExtra("password", sPassWord);
 
startActivity(intent);
 
}
};
}

SecondActivity.java

package com.example.testsharedpreferencesdemo001;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class SecondActivity extends Activity
{
String userName;
String passWord;
TextView info;
Button btn_showInfo;
 
@Override
protected void onCreate(Bundle savedInstanceState)
{
 
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_second);
 
info = (TextView) findViewById(R.id.info);
btn_showInfo = (Button) findViewById(R.id.btn_showinfo);
btn_showInfo.setOnClickListener(new OnClickListener()
{
 
@Override
public void onClick(View v)
{
Toast.makeText(SecondActivity.this, userName + ";" + passWord,
Toast.LENGTH_LONG).show();
}
});
Intent intent = this.getIntent();
userName = intent.getStringExtra("username");
passWord = intent.getStringExtra("password");
System.out.println(userName);
System.out.println(passWord);
 
}
 
}

MainActivity



点击按钮,跳转到SecondActivty,再次点击按钮showinfo



点击“返回键”

回到MainAcitivy
可以看到用户名与密码均保留着


SharedPrefernces使用实例讲解的更多相关文章

  1. float实例讲解

    float实例讲解 float是个强大的属性,在实际前端开发过程中,人们经常拿它来进行布局,但有时,使用的不好,也麻烦多多啊. 比如,现在我们要实现一个两列布局,左边的列,宽度固定:右边的列,宽度自动 ...

  2. S3C2440上RTC时钟驱动开发实例讲解(转载)

    嵌入式Linux之我行,主要讲述和总结了本人在学习嵌入式linux中的每个步骤.一为总结经验,二希望能给想入门嵌入式Linux的朋友提供方便.如有错误之处,谢请指正. 共享资源,欢迎转载:http:/ ...

  3. 实例讲解Oracle数据库设置默认表空间问题

    实例讲解Oracle数据库设置默认表空间问题   实例讲解Oracle数据库设置默认表空间问题,阅读实例讲解Oracle数据库设置默认表空间问题,DBA们经常会遇到一个这样令人头疼的问题:不知道谁在O ...

  4. 基于tcpdump实例讲解TCP/IP协议

    前言 虽然网络编程的socket大家很多都会操作,但是很多还是不熟悉socket编程中,底层TCP/IP协议的交互过程,本文会一个简单的客户端程序和服务端程序的交互过程,使用tcpdump抓包,实例讲 ...

  5. makefile基础实例讲解 分类: C/C++ 2015-03-16 10:11 66人阅读 评论(0) 收藏

    一.makefile简介 定义:makefile定义了软件开发过程中,项目工程编译链.接接的方法和规则. 产生:由IDE自动生成或者开发者手动书写. 作用:Unix(MAC OS.Solars)和Li ...

  6. 实例讲解Linux系统中硬链接与软链接的创建

    导读 Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接.硬链接与软链接的区别从根本上要从Inode节点说 ...

  7. spring事务传播机制实例讲解

    http://kingj.iteye.com/blog/1680350   spring事务传播机制实例讲解 博客分类:   spring java历险     天温习spring的事务处理机制,总结 ...

  8. 实例讲解MySQL联合查询

    好了终于贴完了MySQL联合查询的内容了,加上上一篇一共2篇,都是我转载的,实例讲解MySQL联合查询.那下面就具体讲讲简单的JOIN的用法了.首先我们假设有2个表A和B,他们的表结构和字段分别为: ...

  9. Html代码seo优化最佳布局实例讲解

    搜索引擎对html代码是非常优化的,所以html的优化是做好推广的第一步.一个符合seo规则的代码大体如下界面所示. 1.<!–木庄网络博客–> 这个东西是些页面注释的,可以在这里加我的& ...

随机推荐

  1. 在一周内学会使用 AUTO CAD

    学习目的: 1.使用AUTO CAD绘制电路板外框.元器件封装: 2.借助软件使用,对产品结构有更深入的体会. 学习过程: 1.由于本人急需在短时间内具备简单的二维绘图能力,故没有借鉴.对比网络上其他 ...

  2. AC日记——数字统计 openjudge 1.5 41

    41:数字统计 总时间限制:  1000ms 内存限制:  65536kB 描述 请统计某个给定范围[L, R]的所有整数中,数字2出现的次数. 比如给定范围[2, 22],数字2在数2中出现了1次, ...

  3. MipMap

    MipMap 首先从MIPMAP的原理说起,它是把一张贴图按照2的倍数进行缩小.直到1X1.把缩小的图都存储起来.在渲染时,根据一个像素离眼睛为之的距离,来判断从一个合适的图层中取出texel颜色赋值 ...

  4. Maven学习(八)继承和聚合

    *聚合(多模块) 在一个项目中 往往有多个模块组成,例如有项目demo下面有a, b两个模块 为了能使用一条命令就能构建demo-a, demo-b两个模块, 需要创建一个额外的聚合模块, 然后通过该 ...

  5. HTML 学习笔记(URL)

    URL也被称为网址 URL可以由单词组成,比如"www.baidu.com" 或者是因特网协议IP地址:192.168.1.253.大多数人在网上冲浪时,会键入网址的域名,因为名称 ...

  6. 重写setTimeout扩展参数

    //判断函数行参长度来决定是否需要重写setTimeout,ie8以下为undefined if(window.setTimeout.length == undefined){ var __sto = ...

  7. linux系统root密码遗忘的情况下的解决办法

    机房一台centos系统的服务器,由于这台服务器的系统装了好长时间,且root密码中间更新过几次,后面去机房现场维护时,登陆密码遗忘了,悲催啊~ 没办法,只能开机进入“单用户模式”进行密码重置了. 下 ...

  8. JS常用自定义方法

    1,JS生成随机数方法 getRandom(100),表示生成0-100的数 function getRandom(n){ return Math.floor(Math.random()*n+1) } ...

  9. 64位MicrosoftOfficeWord加载EndnoteX7

    来源:http://jingyan.baidu.com/article/fcb5aff7a08036edaa4a71d0.html Win10 64bit 安装 Office2016 64bit 加载 ...

  10. MathType 公式后的空格问题

    注册表编辑器修改 Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Design Science\DSMT6\WordC ...