Android的读/写用户preferences [英] android read/write user preferences

查看:106
本文介绍了Android的读/写用户preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我移植iOS应用到Android,并思考iOS的,我设置用绳子和钥匙被然后用修改NSUserDefaults的一个默认的plist文件。我看了一些帖子和文档,以及如何正确地做到这在Android中并不真正清楚。
我需要一个preferences连键和字符串,我可以读取和写入文件。谢谢你。


解决方案

 共享preferences设置;
设置= getShared preferences(preF_NAME,Context.MODE_PRIVATE);//获得每股preF
INT ID = settings.getInt(ID,0);//设置共享preF
编辑编辑= settings.edit();
editor.putInt(ID,1);
editor.commit();

I am migrating iOS app to android and, thinking about iOS, I set a default plist file with strings and keys that are then modified using NSuserDefaults. I read some posts and docs and not really clear about how to do it properly in Android. I need a preferences file with keys and strings that I could read and write. Thank you.

解决方案

SharedPreferences settings;
settings = getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);

//get the sharepref
int id = settings.getInt("ID", 0);

//set the sharedpref
Editor editor = settings.edit();
editor.putInt("ID", "1");
editor.commit();

这篇关于Android的读/写用户preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆