Android相当于iOS中的NSUserDefaults [英] Android equivalent of NSUserDefaults in iOS
本文介绍了Android相当于iOS中的NSUserDefaults的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想保存一些简单的数据.在iPhone上,我可以使用Objective-I中的 NSUserDefaults
C.
I'd like to save some simple data. On the iPhone, I can do it with NSUserDefaults
in Objective-C.
Android中的类似命令是什么?
What is the similar command here in Android?
我只保存了一些变量,只要安装了应用程序即可重复使用.我不想使用复杂的数据库来做到这一点.
I'm just saving a few variables, to be reused as long as the application is installed. I don't want to use a complicated database just to do that.
推荐答案
这是我找到的最简单的解决方案:
This is the most simple solution I've found:
//--Init
int myvar = 12;
//--SAVE Data
SharedPreferences preferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("var1", myvar);
editor.commit();
//--READ data
myvar = preferences.getInt("var1", 0);
其中上下文"是当前上下文(例如,活动子类中的可能是 this ).
Where 'context' is the current context (e.g. in an activity subclass could be this).
这篇关于Android相当于iOS中的NSUserDefaults的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文