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);
}
}

最新文章

  1. 玩转SQL Server复制回路の变更数据类型、未分区表转为分区表
  2. cocopods 知识集合 及 一个 好的 国外iOS技术翻译站
  3. ssh隧道(端口转发)
  4. jquery选择器效率优化问题
  5. API地图坐标转化(批量转换坐标)
  6. Java学习笔记(一) java介绍
  7. &quot;Principles of Reactive Programming&quot; 之&lt;Actors are Distributed&gt; (2)
  8. C# 多线程的坑 之 代码变序
  9. ThinkPHP - 连贯操作 - 【实现机制】
  10. 微信小程序倒计时
  11. 如何进行系统配置 ——了解DOS下的内存
  12. [SCOI2008]城堡
  13. 第一周pta作业2
  14. phpstorm软件配置端口问题
  15. oracle 实例名,数据库名概念
  16. jdbc元数据 以及自己动手写一个curd框架
  17. Aspose Word.Dll库自带的bug导致The document appears to be corrupted and cannot be loaded 问题处理。
  18. Delphi 操作键盘按下和释放操作
  19. Controller - 压力机的设置 - 界面图表分析
  20. 转:iOS7导航栏遮盖问题的解决

热门文章

  1. 06 . Python3入门之IO编程(文件操作)
  2. PowerPC-MPC56xx 启动模式
  3. 最新 iOS 框架整体梳理(一)
  4. 面试官:兄弟,说说Java的static关键字吧
  5. Java实现 LeetCode 462 最少移动次数使数组元素相等 II
  6. Java实现 蓝桥杯VIP 基础练习 时间转换
  7. Java实现 蓝桥杯VIP 算法提高 陶陶摘苹果2
  8. Java实现 蓝桥杯VIP 算法提高 最长公共子序列
  9. 第七届蓝桥杯JavaA组国(决)赛部分真题
  10. Android中如何使用多选对话框