Webview的SharedPreferences方法示例
时间: 2024-06-04 07:12:58 浏览: 7
Sure, here is an example of using SharedPreferences with a Webview in Android:
// Create SharedPreferences object
SharedPreferences sharedPref = getSharedPreferences("webview_prefs", Context.MODE_PRIVATE);
// Get value by key
String username = sharedPref.getString("username", "");
// Set value by key
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("username", "JohnDoe");
editor.apply();
In this example, we create a SharedPreferences object with a unique name ("webview_prefs") and mode (private). We then get the value stored under the key "username" ("") and store it in a String variable. Finally, we set the value of "username" to "JohnDoe" using an editor and apply the changes.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)