Android的共享preferences,如何拯救一个简单的int变量 [英] Android SharedPreferences , how to save a simple int variable

查看:97
本文介绍了Android的共享preferences,如何拯救一个简单的int变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在最后一小时,以节省整在我的Andr​​oid应用程序。我看,这可以通过使用共享preferences来完成。但是我不明白为什么它似乎很混乱的话。

我怎么能简单地保存一个int变量?然后,当我再次运行应用程序,我怎么能再次与这个变量进行交互?

解决方案

 共享preferences SP = getShared preferences(your_ preFS,Activity.MODE_PRIVATE );
共享preferences.Editor编辑器= sp.edit();
editor.putInt(your_int_key,yourIntValue);
editor.commit();
 

在你可以得到它的:

 共享preferences SP = getShared preferences(your_ preFS,Activity.MODE_PRIVATE);
 INT myIntValue = sp.getInt(your_int_key,-1);
 

共享preference 接口,您可以访问到一个XML文件,以及简单的方式通过编辑器修改它。该文件存储在 /data/data/com.your.package/shared_$p$pfs / 键,就可以通过这个访问onlu 共享preference API

I am trying for the last hour to save an integer in my Android application. I read that this can be done using the SharedPreferences. However i dont understand why it seems so confusing to do so.

How can i simply save an int variable ? And then when i run the application again , how can i interact with this variable again?

解决方案

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("your_int_key", yourIntValue);
editor.commit();

the you can get it as:

 SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
 int myIntValue = sp.getInt("your_int_key", -1);

The SharedPreference interface gives you access to the an xml file, and a easy way to modify it through its editor. The file is stored in /data/data/com.your.package/shared_prefs/ and you can access it onlu through this SharedPreference API

这篇关于Android的共享preferences,如何拯救一个简单的int变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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