简体   繁体   English

Android SharedPreferences

[英]Android SharedPreferences

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. 我的搜索引擎应用程序中有3个Web视图。默认情况下,当用户输入查询时,他会获取Google搜索结果,然后在底部点击yahoo和Ask按钮,然后单击其中的任意一个,即可从这些站点获取该查询的结果。 Now I want to give the user the privilege to change the default result site. 现在,我想授予用户更改默认结果站点的权限。 I have created 3 radiobuttons. 我创建了3个单选按钮。 Upon confirmation, say he chooses Yahoo, how can i set it as Yahoo till the next time he changes it to some other site, 确认后,说他选择了Yahoo,我如何将其设置为Yahoo,直到他下次将其更改为其他网站时,

Accessing data from SharedPreferences: 从SharedPreferences访问数据:

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

if no choice was saved (in the case when the application is running for the first time), you'll get "null" in webViewChoice . 如果未保存任何选择(在应用程序首次运行的情况下),则webViewChoice “ null”。 use this condition as you wish 根据需要使用此条件

Saving data in SharedPreferences: 在SharedPreferences中保存数据:

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

I hope it helps. 希望对您有所帮助。

Save the user's preference as default-engine=google by default in a shared preferences file. 默认情况下,在共享的首选项文件中将用户的首选项保存为default-engine = google。 On app loading, read the file and set the default engine during the app runtime. 在加载应用时,请在应用运行时读取文件并设置默认引擎。 When user chooses a different engine as default, then update the preferences file. 当用户选择其他引擎作为默认引擎时,然后更新首选项文件。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM