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(); } }); }
如对本文有疑问, 点击进行留言回复!!
Android安卓水滴屏适配状态栏图标(图标过多时显示一个点)
android studio使用Flutter创建web项目
android如何使用taskAffinity属性对任务栈操作控制
android对PDF文件的操作(上传、预览、下载和转存相册)
Android通过代码批量生成字符图标用于给输入法显示状态栏的图标
网友评论