android使用sharepreference保存用户操作数据
为什么80%的码农都做不了架构师?>>>
很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友。对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存,如果是j2se应用,我们会采用properties属性文件进行保存。如果是Android应用,我们最适合采用什么方式保存软件配置参数呢?Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下
资源目录图:
布局layout代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/name"/><EditText android:id="@+id/name"android:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="@string/name"/><TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/age"/><EditTextandroid:id="@+id/age"android:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="@string/age" /><Button android:id="@+id/save"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/saveBtn"android:onClick="save"/>
</LinearLayout>
MainActivity.java代码:
package org.dyb.sharepreferencedemo;import java.util.Map;import org.dyb.service.PreferencesService;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;public class MainActivity extends Activity {private EditText nameEdit;private EditText ageEdit;PreferencesService service;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);nameEdit=(EditText) findViewById(R.id.name);ageEdit=(EditText) findViewById(R.id.age);//saveBtn=(Button) findViewById(R.id.save);service=new PreferencesService(this);//回显Map<String,String> map=service.getPreferences();nameEdit.setText(map.get("name"));ageEdit.setText(map.get("age"));}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}public void save(View v){String nameString=nameEdit.getText().toString();String ageString=ageEdit.getText().toString();service.save(nameString, Integer.valueOf(ageString));Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();}}
PreferencesService.java代码:
package org.dyb.service;import java.util.HashMap;
import java.util.Map;import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;public class PreferencesService {private Context context;public PreferencesService(){}public PreferencesService(Context context){this.context=context;}public void save(String name,int age){SharedPreferences preferences=context.getSharedPreferences("ddd", Context.MODE_PRIVATE);Editor edit=preferences.edit();//数据是放在内存中的edit.putString("name", name);edit.putInt("age", age);//提交方法,把内存中的数据提交到文件中edit.commit();}//获取保存的文件内容public Map<String,String> getPreferences(){Map<String,String> param=new HashMap<String, String>();SharedPreferences preferences=context.getSharedPreferences("ddd", Context.MODE_PRIVATE);param.put("name", preferences.getString("name", ""));param.put("age", String.valueOf(preferences.getInt("age", 0)));return param;}
}
布局效果:
保存在ddd.xml中的数据:
转载于:https://my.oschina.net/zjcx/blog/679557
android使用sharepreference保存用户操作数据相关推荐
- Android 利用sharepreference保存应用程序状态
在开发Android应用过程中,都会需要保存应用程序的状态,比如音乐播放器利用sharepreference保存用户的设置,音乐的播放状态,音乐的播放进度等.就在现在的项目上需要对主界面上按钮进行控制 ...
- Android学习笔记---16_采用SharedPreferences保存用户偏好设置参数
16_采用SharedPreferences保存用户偏好设置参数 Android学习笔记---16_采用SharedPreferences保存用户偏好设置参数 2013-03-08 16_采用Shar ...
- Android 自定义ToggleButton+用SharedPreferences保存用户配置
布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:i ...
- Android基础入门教程——6.2 数据存储与访问之——SharedPreferences保存用户偏好参数
Android基础入门教程--6.2 数据存储与访问之--SharedPreferences保存用户偏好参数 标签(空格分隔): Android基础入门教程 本节引言: 本节给大家介绍的是第二种存储用 ...
- android 通过GPS获取用户地理位置并监听位置变化
1 Location Manager 管理服务 2 Location Provider 提供数据的content provider 方式一:GPS 特点:精度高,耗电量大,不耗费流量 权限<us ...
- 移动App该怎样保存用户password
版权声明:本文为横云断岭原创文章,未经博主同意不得转载.微信公众号:横云断岭的专栏 https://blog.csdn.net/hengyunabc/article/details/34623957 ...
- android view 存值,在android studio中保存textview和复选框的值
问:如何在android studio中保存文本视图和复选框的值,以便稍后我进入该活动时以及在设置新值然后保持新值时可见设置的数据. 答:SharedPreferences用于在Android中存储原 ...
- android 客户端使用service处理用户名和密码验证,android透过webservice验证用户
当前位置:我的异常网» Web前端 » android透过webservice验证用户 android透过webservice验证用户 www.myexceptions.net 网友分享于:2015 ...
- 移动App该如何保存用户密码
这个实际上和桌面程序是一样的. 先看下一些软件是如何保存用户密码的: 我们先来看下QQ是怎么保存密码的: 参考:http://bbs.pediy.com/archive/index.php?t-159 ...
最新文章
- centos 安装 rabbitMq
- flutter git 拉不起来_「干货」Flutter开发环境配置备忘录
- keras从入门到放弃(七)多层感知器训练
- PCA原理分析和Matlab实现方法(三)
- Nginx的http块其他的配置指令说明
- 全场灭灯_海天黄豆酱《新相亲大会2》首遇滑铁卢,男嘉宾险遭全场灭灯
- Android 日夜间切换Demo
- 用数组存储循环序号的数据
- Windows下的Qt Creator的安装
- 登录界面转换实现html,html5和css3登录注册表单界面切换动画
- STM32系列芯片名称定义
- JAVA秒杀mysql层实现_一文搞懂MySQL的Join,聊一聊秒杀架构设计
- 快讯:迈阿密在建公寓计划为飞行汽车推出天空港口
- 商品规格参数数据库设计
- 计算机硬盘存储器相关概念
- 机器学习-sklearn第十二天——笔记
- 收发器(Transceiver)架构1——接收机1
- android 录像 视频大小,Android相机 – 录制视频时预览放大
- Rasa课程、Rasa培训、Rasa面试、Rasa实战系列之Toxic Language Detection
- Buffon投针问题