黎活明8天快速掌握android视频教程--16_采用SharedPreferences保存用户偏好设置参数
2024-10-09 07:49:02
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);
}
}
最新文章
- 玩转SQL Server复制回路の变更数据类型、未分区表转为分区表
- cocopods 知识集合 及 一个 好的 国外iOS技术翻译站
- ssh隧道(端口转发)
- jquery选择器效率优化问题
- API地图坐标转化(批量转换坐标)
- Java学习笔记(一) java介绍
- ";Principles of Reactive Programming"; 之<;Actors are Distributed>; (2)
- C# 多线程的坑 之 代码变序
- ThinkPHP - 连贯操作 - 【实现机制】
- 微信小程序倒计时
- 如何进行系统配置 ——了解DOS下的内存
- [SCOI2008]城堡
- 第一周pta作业2
- phpstorm软件配置端口问题
- oracle 实例名,数据库名概念
- jdbc元数据 以及自己动手写一个curd框架
- Aspose Word.Dll库自带的bug导致The document appears to be corrupted and cannot be loaded 问题处理。
- Delphi 操作键盘按下和释放操作
- Controller - 压力机的设置 - 界面图表分析
- 转:iOS7导航栏遮盖问题的解决