Android的共享preferencesAndroid、preferences

2023-09-05 01:19:12 作者:渲染死亡的华丽。

我有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.