黎活明8天快速掌握android视频教程--16_采用SharedPreferences保存用户偏好设置参数
SharedPreferences保存的数据是xml格式,也是存在数据保存的下面四种权限:

我们来看看

我们来看看具体的业务操作类:
/**
* 文件名:SharedPrecences.java
* 版权:版权所有 (C) 中国电科30所三部
* 描述:
* 修改人: wei.yuan
* 修改时间:2015/1/8
* 修改内容:新增
*/
package service; import android.content.Context;
import android.content.SharedPreferences; import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map; /**
* 项目名称:SharedPreferences
* 类描述:
* 创建人:wei.yuan
* 创建时间:2015/1/8 10:29
* 修改人:wei.yuan
* 修改时间:2015/1/8 10:29
* 修改备注:
* 版权:版权所有 (C) 中国电科30所三部
*/
public class SharedPrecences {
private Context context; public SharedPrecences(Context context) {
this.context = context;
}
public void saveSharedPrecences(String name ,Integer age)
{
SharedPreferences sharedPrecences = context.getSharedPreferences("123", context.MODE_PRIVATE);//默认的格式是xml,123就不需要写后缀名
SharedPreferences.Editor editor = sharedPrecences.edit();//编辑文字
editor.putString("name",name);
editor.putInt("age", age);
editor.commit();//保存之后记得提交 }
/*
public void save(String name , Integer age) throws Exception{
SharedPreferences preference = context.getSharedPreferences("sclead", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preference.edit();
editor.putString("name", name);
editor.putInt("age", age);
editor.commit();//把数据提交会文件
}
*/ public Map<String ,String> show()
{
Map<String,String > map = new Hashtable<String, String>();
SharedPreferences sharedPrecences = context.getSharedPreferences("123", context.MODE_PRIVATE);//默认的格式是xml,123就不需要写后缀名
map.put("name",sharedPrecences.getString("name","查找的字段不存在"));
map.put("age",String.valueOf(sharedPrecences.getInt("age", 0))); return map;
}
/*
*/
/**
* 获取各项配置参数
* @return
*//* public Map<String, String> getPreferences(){
Map<String, String> maps = new HashMap<String, String>();
SharedPreferences preference = context.getSharedPreferences("sclead", Context.MODE_PRIVATE);
maps.put("name", preference.getString("name", "你查找的字段不存在"));
maps.put("age", String.valueOf(preference.getInt("age", 0)));
return maps;
}
*/ }
我们来看看activity的代码是:
package test.weiyuan.sharedpreferences; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import java.util.Map; import service.SharedPrecences; public class MyActivity extends Activity {
private EditText name1,age1;
private TextView showText;
private Button saveButton ,showButton; SharedPrecences sharedPrecences = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
name1 = (EditText)this.findViewById(R.id.name);
age1 = (EditText)this.findViewById(R.id.age);
saveButton = (Button)this.findViewById(R.id.savebutton);
showButton = (Button)this.findViewById(R.id.showButton);
showText = (TextView)this.findViewById(R.id.showText); sharedPrecences = new SharedPrecences(getApplicationContext()); saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { try {
/* sharedPrecences.save(name1.getText().toString(), Integer.valueOf(age1.getText().toString()));*/
String str = name1.getText().toString();
String str1 = age1.getText().toString();
sharedPrecences.saveSharedPrecences(str, Integer.valueOf(str1));
Toast.makeText(getApplicationContext(), "数据保存成功", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
} }
});
showButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Map<String,String> map = sharedPrecences.show();
name1.setText(map.get("name"));
Log.i("wy",map.get("name"));
showText.setText(map.get("name")+map.get("age"));
} });
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
黎活明8天快速掌握android视频教程--16_采用SharedPreferences保存用户偏好设置参数的更多相关文章
- 黎活明8天快速掌握android视频教程--12_文件的保存与读取
1.当前是把文件保存当前手机的app的data目录下 我们来看看操作保存文件的业务类 package contract.test.savafileapplication; import android ...
- 黎活明8天快速掌握android视频教程--20_采用ContentProvider对外共享数据
1.内容提供者是让当前的app的数据可以让其他应用访问,其他应该可以通过内容提供者访问当前app的数据库 contentProvider的主要目的是提供一个开发的接口,让其他的应该能够访问当前应用的数 ...
- 黎活明8天快速掌握android视频教程--19_采用ListView实现数据列表显示
1.首先整个程序也是采用mvc的框架 DbOpenHelper 类 package dB; import android.content.Context; import android.databas ...
- 黎活明8天快速掌握android视频教程--15_采用Pull解析器解析和生成XML内容
1.该项目主要有下面的两个作用 (1)将xml文件解析成对象的List对象,xml文件可以来自手机本地,也可以来自服务器返回的xml数据 (2)强list对象保存成xml文件,xml保存到手机的内存卡 ...
- 黎活明8天快速掌握android视频教程--22_访问通信录中的联系人和添加联系人
Android系统中联系人的通讯录的contentProvide是一个单独的apk,显示在界面的contact也是一个独立的apk,联系人apk通过contentProvide访问底层的数据库. 现在 ...
- 黎活明8天快速掌握android视频教程--14_把文件存放在SDCard
把文件保存在手机的内部存储空间中 1 首先必须在清单文件中添加权限 <?xml version="1.0" encoding="utf-8"?> & ...
- 黎活明8天快速掌握android视频教程--27_网络通信之通过GET和POST方式提交参数给web应用
1该项目主要实现Android客户端以get的方式或者post的方式向java web服务器提交参数 Android客户端通过get方式或者post方式将参数提交给后台服务器,后台服务器对收到的参数进 ...
- 黎活明8天快速掌握android视频教程--25_网络通信之资讯客户端
1 该项目的主要功能是:后台通过xml或者json格式返回后台的视频资讯,然后Android客户端界面显示出来 首先后台新建立一个java web后台 采用mvc的框架 所以的servlet都放在se ...
- 黎活明8天快速掌握android视频教程--24_网络通信之网页源码查看器
1 该项目的主要功能就是从将后台的html网页在Android的界面上显示出来 后台就是建立一个java web工程在工程尚建立一个html或者jsp文件就可以了,这里主要看Android客户端的程序 ...
随机推荐
- 计划任务工具-windows
计划任务工具根据自己设定的具体时间,频率,命令等属性来规定所要执行的计划. 代码 # -*- coding: utf-8 -*- """ Module implement ...
- 选择器&隔行换色
选择器的使用理解为:执行jQuery核心函数,传入选择器的字符串 $( ... ) 基本选择器 <!DOCTYPE html> <html> <head> & ...
- 创建并加入节点&练习
1.节点的属性 节点的属性:所有节点都有的属性 元素节点, 属性节点, 文本节点 nodeType 只 读 属 性 nodeName 返回对应节点的名字 ...
- Bank5
Account: package banking5; //账户 public class Account { protected double balance; public Account(doub ...
- 接口(interface)的使用
类实现接口就具有接口的功能 实现可以多实现,实现多个接口 package cm.aff.abst; /* 接口(interface)是与类并行的一个概念 1. 接口可以看做是一个特殊的抽象类,是常量与 ...
- C/C++多参数函数参数的计算顺序与压栈顺序
一.前言 今天在看Thinking in C++这本书时,书中的一个例子引起了我的注意,具体是使用了下面这句 单看这条语句的语义会发现仅仅是使用一个简单的string的substr函数将所得子串pus ...
- Rocket - tilelink - Monitor
https://mp.weixin.qq.com/s/6e-G5RSQc7Xje7mQj8-Lag 简单介绍Monitor的实现. 1. 基本介绍 用于监控各个channel上的 ...
- jchdl - RTL实例 - AndReg
https://mp.weixin.qq.com/s/p4-379tBRYKCYBk8AZoT8A 输入两组线相与,结果输出到寄存器. 参考链接 https://github.com/wjcd ...
- 学习源码的第八个月,我成了Spring的开源贡献者
@ 目录 我的经历 碰到的问题 1.担心闹乌龙 2.不知道要怎么提交 3.英文 4.担心问题描述的不清楚 给你的建议 我的经历 关注我的朋友都知道,关注两个字划重点,要考! 我最近一直在写Spring ...
- js匿名函数和date对象,math对象
匿名函数: <script type="text/javascript"> function (参数列表){ 要执行的语句块; } </script> 对象 ...