[英]android: how to save my user interface and get it back after reboot?
Sorry I am totally new to Android 抱歉,我是Android的新手
My application has a very simple GUI (2 checkboxes, 3 textiputs). 我的应用程序具有非常简单的GUI(2个复选框,3个文本输入)。 I want to save the value of these widgets (checkboxes, textinputs), so after I reboot my andorid they keep their values.
我想保存这些小部件(复选框,文本输入)的值,因此在重新启动Andorid之后,它们将保留其值。
I guess I can use some local stoarge or db storage, but I am not fluent enough to pick up the fastest/better solution to do that? 我想我可以使用一些本地存储或数据库存储,但是我不够流利,无法选择最快/更好的解决方案来做到这一点?
Regards 问候
Here is the documentation for storing data in Android 这是用于在Android中存储数据的文档
Since you are working with a small amount of data to be saved, I'd recommend option #1: Saving Key-Value Sets aka SharedPreferences
由于您正在处理少量要保存的数据,因此我建议您选择选项1: 保存键值集 (也称为
SharedPreferences
You can do it as follows 您可以按照以下步骤进行操作
Writing to Shared Preferences 写入共享首选项
SharedPreferences shp = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shp.edit();
editor.putString("YourPreferenceName", yourVariable);
editor.commit();
Reading from Shared Preferences 从共享首选项中读取
SharedPreferences shp = getActivity().getPreferences(Context.MODE_PRIVATE);
String yourVariable= shp.getString"YourPreferenceName", "default");
Store the values in SharedPreferences
. 将值存储在
SharedPreferences
。 Alternatively, read the page on Settings screens (which use SharedPreference
s behind the scenes). 或者,阅读“ 设置”屏幕上的页面 (在后台使用
SharedPreference
)。
You can use PreferenceFragment which can automagically store those values in the SharedPreferences . 您可以使用PreferenceFragment来自动将这些值存储在SharedPreferences中 。 Here is a good tutorial .
这是一个很好的教程 。 If thats all you need to store locally, using
PreferenceFragment
will save you time coding saving/retrieving/displaying those values from SharedPreferences
yourself. 如果这就是您需要在本地存储的全部内容,则使用
PreferenceFragment
可以节省您自己编写,保存/检索/显示来自SharedPreferences
值的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.