SharedPrefernces使用实例讲解
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" ><EditTextandroid:id="@+id/userName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="Please input your username" /><EditTextandroid:id="@+id/passWord"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="Please input your password"android:inputType="textPassword" /><Buttonandroid: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" ><TextViewandroid:id="@+id/info"android:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid: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")@Overrideprotected 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(){@Overridepublic 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;@Overrideprotected 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(){@Overridepublic 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

点击“返回键”

SharedPrefernces使用实例讲解的更多相关文章
- float实例讲解
float实例讲解 float是个强大的属性,在实际前端开发过程中,人们经常拿它来进行布局,但有时,使用的不好,也麻烦多多啊. 比如,现在我们要实现一个两列布局,左边的列,宽度固定:右边的列,宽度自动 ...
- S3C2440上RTC时钟驱动开发实例讲解(转载)
嵌入式Linux之我行,主要讲述和总结了本人在学习嵌入式linux中的每个步骤.一为总结经验,二希望能给想入门嵌入式Linux的朋友提供方便.如有错误之处,谢请指正. 共享资源,欢迎转载:http:/ ...
- 实例讲解Oracle数据库设置默认表空间问题
实例讲解Oracle数据库设置默认表空间问题 实例讲解Oracle数据库设置默认表空间问题,阅读实例讲解Oracle数据库设置默认表空间问题,DBA们经常会遇到一个这样令人头疼的问题:不知道谁在O ...
- 基于tcpdump实例讲解TCP/IP协议
前言 虽然网络编程的socket大家很多都会操作,但是很多还是不熟悉socket编程中,底层TCP/IP协议的交互过程,本文会一个简单的客户端程序和服务端程序的交互过程,使用tcpdump抓包,实例讲 ...
- makefile基础实例讲解 分类: C/C++ 2015-03-16 10:11 66人阅读 评论(0) 收藏
一.makefile简介 定义:makefile定义了软件开发过程中,项目工程编译链.接接的方法和规则. 产生:由IDE自动生成或者开发者手动书写. 作用:Unix(MAC OS.Solars)和Li ...
- 实例讲解Linux系统中硬链接与软链接的创建
导读 Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接.硬链接与软链接的区别从根本上要从Inode节点说 ...
- spring事务传播机制实例讲解
http://kingj.iteye.com/blog/1680350 spring事务传播机制实例讲解 博客分类: spring java历险 天温习spring的事务处理机制,总结 ...
- 实例讲解MySQL联合查询
好了终于贴完了MySQL联合查询的内容了,加上上一篇一共2篇,都是我转载的,实例讲解MySQL联合查询.那下面就具体讲讲简单的JOIN的用法了.首先我们假设有2个表A和B,他们的表结构和字段分别为: ...
- Html代码seo优化最佳布局实例讲解
搜索引擎对html代码是非常优化的,所以html的优化是做好推广的第一步.一个符合seo规则的代码大体如下界面所示. 1.<!–木庄网络博客–> 这个东西是些页面注释的,可以在这里加我的& ...
随机推荐
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- Miller-Rabin素数快速检测
满足费马小定理 a^(n-1) === 1(mod n) --->伪素数 对于所有a belong Zn*,总存在满足的合数n,称为Carmichael数 ------------- ...
- 平滑过渡的战争迷雾(一) 原理:Warcraft3地形拼接算法
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9611887 作者:七十一雾央 新浪微博:http:/ ...
- [No000032]程序员的年龄天花板
程序员职业生涯中流行这这样一个定律:35岁定律,那35岁以上的老程序员都干吗去了呢?为了讨论程序员的职业寿命,我们先得给公司或者团队分分类.大概有这么三类: 外包型 项目型 产品型 咱们一一来说一下吧 ...
- 判断一个变量的类型Object.prototype.toString.call
var num = 1;alert(Object.prototype.toString.call(num)); // [object Number]var str = 'hudidit.com';al ...
- android edittext 去边框 去下划线
EditText的background属性设置为@null就搞定了:android:background="@null"style属性倒是可加可不加 附原文:@SlumberMac ...
- 表单 - Validatebox - 表单参数校验
$("input[name='username']").validatebox({ required: true,//必填 validType:'email'//要求用户名必须是一 ...
- iOS开发 传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- struts2: 玩转 rest-plugin
近期使用struts2的rest-plugin,参考官方示例struts2-rest-showcase,做了一个restful service小项目,但官网提供的这个示例过于简单,埋下了巨坑无数,下面 ...
- Android的媒体管理框架:Glide
Glide是一个高效.开源. Android设备上的媒体管理框架,它遵循BSD.MIT以及Apache 2.0协议发布.Glide具有获取.解码和展示视频剧照.图片.动画等功能,它还有灵活的API,这 ...