Android 共享参数 SharedPreferences
程序员文章站
2022-05-09 16:10:08
完成共享参数的读写public class SharedPreference { private Context context; public SharedPreference(Context context) { // TODO Auto-generated constructor stub t... ......
完成共享参数的读写
public class sharedpreference { private context context; public sharedpreference(context context) { // todo auto-generated constructor stub this.context = context; } public boolean savemessage(string name, string passwd) { boolean flag = false; // 自动保存成 userinfo.xml sharedpreferences sharedpreferences = context.getsharedpreferences("userinfo", context.mode_private); // 对数据进行编辑 sharedpreferences.editor editor = sharedpreferences.edit(); editor.putstring("name", name); editor.putstring("passwd", passwd); // 将数据持久化到存储介质中 flag = editor.commit(); return flag; } public map<string, object> getmessage() { map<string, object> map = new hashmap<string, object>(); sharedpreferences sharedpreferences = context.getsharedpreferences("userinfo", context.mode_private); string name = sharedpreferences.getstring("name", ""); string passwd = sharedpreferences.getstring("passwd", ""); map.put("name", name); map.put("passwd", passwd); return map; } }
编写测试函数
private button button1; private button button2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button1 = (button) findviewbyid(r.id.button1); button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub sharedpreference sharedpreference = new sharedpreference(mainactivity.this); boolean flag = sharedpreference.savemessage("furong", "123456"); toast.maketext(mainactivity.this, "---->" + flag, 1).show(); } }); button2 = (button) findviewbyid(r.id.button2); button2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub map<string, object> map; sharedpreference sharedpreference = new sharedpreference(mainactivity.this); map = sharedpreference.getmessage(); toast.maketext(mainactivity.this, map.tostring(), 1).show(); } }); }
写测试
读测试
上一篇: 首款可超频Zen速龙!AMD发布速龙3000G APU:同级别唯一
下一篇: 先留你狗命
推荐阅读
-
Android中Activity之间跳转和参数传递的实例
-
Android实现不同apk间共享数据的方法(2种方法)
-
Android持久化技术之SharedPreferences存储实例详解
-
Android中Activity之间跳转和参数传递的实例
-
android使用SharedPreferences进行数据存储
-
android与asp.net服务端共享session的方法详解
-
Android SharedPreferences实现记住密码和自动登录界面
-
Android SharedPreferences存储用法详解
-
Android中SharedPreferences简单使用实例
-
android使用SharedPreferences进行数据存储