Android的共享preferences [英] Android SharedPreferences

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

问题描述

我有3 webviews在我的搜索引擎app.When用户输入自己的查询,在默认情况下,他得到谷歌的结果,其次是雅虎和Ask按键在底部,在点击其中任何一个,他得到的结果从该查询这些网站。现在,我想给用户的权限更改默认结果网站。我创建了3单选按钮。确认后,说他选择了雅虎,我怎么可以将其设置为雅虎,直到他改变它的一些其他网站,下一次,

I have 3 webviews in my search engine app.When the user enters his query, by default he gets Google results, followed by yahoo and Ask buttons at the bottom, on clicking either of them, he gets results for that query from those sites. Now I want to give the user the privilege to change the default result site. I have created 3 radiobuttons. Upon confirmation, say he chooses Yahoo, how can i set it as Yahoo till the next time he changes it to some other site,

推荐答案

从共享preferences访问数据:

Accessing data from SharedPreferences:

SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
String webViewChoice = sharedPref.getString("userChoice","null");

如果(在应用程序运行在第一次的情况下),没有选择保存,你会得到空在 webViewChoice 。 利用这个条件,如你所愿

if no choice was saved (in the case when the application is running for the first time), you'll get "null" in webViewChoice. use this condition as you wish

在共享preferences保存数据:

Saving data in SharedPreferences:

SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("userChoice",usersChoice);
prefEditor.commit();

我希望它能帮助。

I hope it helps.

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

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