添加preference值 [英] Adding Preference Values

查看:100
本文介绍了添加preference值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在Android的加入2 preference值?即考虑一个游戏,有硬币获得生命,并说,一个用户选择不使用硬币和游戏启动一次again.Now可以说,他有几个coins.So我的问题是如何添加旧的非利用硬币+新获得的硬币?

I would like to know how to add 2 preference values in android?ie consider a game which has coins to gain life and say an user choose not to use that coins and starts game once again.Now lets say he got few more coins.So my question is how to add old non utilized coins+newly obtained coins?

推荐答案

您可以使用共享preferences

写的共享preferences使用:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

并从共享preferences阅读使用方法:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

这篇关于添加preference值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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