Android保存之SharedPreferences
Android中一共有四种存储方式:
SharedPreferences 为其中的一种,具体还是看代码:
package com.wyl.preferencetest; import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.os.Build;
import android.preference.PreferenceManager; public class MainActivity extends ActionBarActivity {
SharedPreferences pref ;
String username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1 获取到preference的两种方式:
// pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
pref = getSharedPreferences("myOwn", MODE_PRIVATE);
//2.使pref可编辑,获得一个可编辑对象
Editor editor = pref.edit();
editor.putString("yourname", "wyl");
editor.putInt("age", 26);
//3提交编辑,否则不成效的
editor.commit();
editor.putBoolean("flag", true);
editor.putBoolean("flag2", true);
editor.remove("flag");
editor.commit();
String myname = pref.getString("yourname","yourname");
System.out.println("myname:"+myname);
MainActivity.this.username = myname; } public void btnOclick(View v){
if(R.id.btn01==v.getId()){
System.out.println("======"+this.username);
// Toast.makeText(MainActivity.this, username+",你登陆成功了", 1000).show();//一定要加.show()方法
Toast.makeText(MainActivity.this, "this is the toast content", Toast.LENGTH_LONG).show();//
}
} }
效果图如下:
Android保存之SharedPreferences的更多相关文章
- Android应用开发SharedPreferences存储数据的使用方法
Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...
- Android学习之SharedPreferences类
SharedPreferences类 android.content.SharedPreferences 类概括: 访问和修改由函数getSharedPreferences(String,int)返回 ...
- 【Android】数据共享 sharedPreferences 相关注意事项
Android 中通过 sharedPreferences 来持久化存储数据并进行共享 在 Activity 或存在 Context 环境中即可使用 context.getSharedPreferen ...
- Android开发:SharedPreferences 存储数据、获取数据
Android开发:SharedPreferences 存储数据.获取数据 email:chentravelling@163.com 开发环境:win7 64位,Android Studio. 关于S ...
- 【Mark】Android应用开发SharedPreferences存储数据的使用方法
Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...
- wemall app商城源码Android数据的SharedPreferences储存方式
wemall-mobile是基于WeMall的Android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android数据 ...
- Android 中替代 sharedpreferences 工具类的实现
Android 中替代 sharedpreferences 工具类的实现 背景 想必大家一定用过 sharedpreferences 吧!就我个人而言,特别讨厌每次 put 完数据还要 commit. ...
- android:保存用户名密码等应用程序数据
转自http://blog.sina.com.cn/s/blog_a73687bc0101dsjj.html (一)使用SharedPreferences 1.保存信息: SharedPrefere ...
- Android 分享一个SharedPreferences的工具类,方便保存数据
我们平常保存一些数据,都会用到SharedPreferences,他是保存在手机里面的,具体路径是data/data/你的包名/shared_prefs/保存的文件名.xml, SharedPrefe ...
随机推荐
- jQuery.validate 中文 API
名称 返回类型 描述 validate(options) Validator 验证所选的 FORM. valid() Boolean 检查是否验证通过. rules() Options 返回元素的验证 ...
- CentOS5.4下安装codeblocks 12.11
centos6.3下安装codeblock简单多了,这些开源的软件也都在不断进步.原来装过codeblocks10.05,忘了,这次安装又花了我半天时间,最后总算搞定. 先是安装了wxGTK-2.8. ...
- 13-C语言字符串函数库
目录: 一.C语言字符串函数库 二.用命令行输入参数 回到顶部 一.C语言字符串函数库 1 #include <string.h> 2 字符串复制 strcpy(参数1,参数2); 参数1 ...
- (Problem 72)Counting fractions
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...
- 原型扩展的方法解决IE和Firefox的Js兼容问题
if(!document.all){//textContent->text Element.prototype.__defineGetter__('text',function(){ret ...
- Spring学习笔记1——IOC: 尽量使用注解以及java代码(转)
在实战中学习Spring,本系列的最终目的是完成一个实现用户注册登录功能的项目. 预想的基本流程如下: 1.用户网站注册,填写用户名.密码.email.手机号信息,后台存入数据库后返回ok.(学习IO ...
- HDU 1234 开门人和关门人
#include <string> #include <algorithm> #include <iostream> using namespace std; st ...
- software quality assurance 常见问题收录
1. What is Quality? Quality means, “meeting requirements.” ..Whether or not the product or service d ...
- LKD3
第三章 进程1. Unix操作系统的抽象:进程和文件2. 进程包括两个因素:可运行代码,和资源(打开的文件,挂起的信号,内核内部数据,处理器状态,地址空间)3. 线程是进程中活动的对象.4. 线程有独 ...
- iOS中的 SB和XIB的前世今生
今天给大家介绍一下Apple开发中三种几种常用的应用程序编写方式:纯代码创建.使用storyboard/XIB.我们都知道,纯代码编写模式适合大型项目大规模使用,利于版本管理.追踪改动以及代码合并,代 ...