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 ...
随机推荐
- windows文件快速搜索软件推荐
everything文件搜索工具,可以快速搜索windows下的文件
- (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 ...
- tomcat 会话超时设置
1.为单个WEB设置SESSION 在WEB.XML中添加 xml 代码 <session-config> <session-timeout>15</session-ti ...
- Nginx工作原理和优化、漏洞(转)
查看安装了哪些模块命令: [root@RG-PowerCache-X xcache]# nginx/sbin/nginx -Vnginx version: nginx/1.2.3built by gc ...
- 转载Spring IntrospectorCleanupListener
"在服务器运行过程中,Spring不停的运行的计划任务和OpenSessionInViewFilter,使得Tomcat反复加载对象而产生框架并用时可能产生的内存泄漏,则使用Introspe ...
- 介绍一个python的新的web framework——karloop框架
karloop是一款轻型的web framework,和tornado.webpy类似.mvc分层设计,眼下已经公布早期版本号了,使用方便, 下载地址例如以下:https://github.com/k ...
- 【Android病毒分析报告】 - ZxtdPay 吸费恶魔
本文章由Jack_Jia编写,转载请注明出处. 文章链接:http://blog.csdn.net/jiazhijun/article/details/11581543 作者:Jack_Jia ...
- uestc 250 数位dp(水)
/* 数位dp 水题 开两维一个记录长度,一个记录上一个数 */ #include<stdio.h> #include<string.h> #define N 13 int d ...
- Oracle_sqlload导数案例
文件地址:http://115.com/lb/5lbbut5jc6op 案例中的sql_load导数公用到5个文件,分别是bat.ctl.txt.log.bad 5个文件 bat文件: --用户名/用 ...
- Oracle 创建用户并且授权
以sysdba登陆 创建用户:CREATE USER username IDENTIFIED BY password; 授予(角色)权限:GRANT CONNECT,RESOURCE TO usern ...