日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Andriod官方训练教程]保存数据之保存键-值对的集合

發(fā)布時間:2023/12/10 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Andriod官方训练教程]保存数据之保存键-值对的集合 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

原文地址:https://developer.android.com/training/basics/data-storage/shared-preferences.html

-------------------------------------------------------------------------------------------------------------------------------

If you have a relatively small collection of key-values that you'd like to save, you should use theSharedPreferences APIs. ASharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. EachSharedPreferences file is managed by the framework and can be private or shared.

如果你有一個相對較小的鍵-值對集合想要存儲,你應(yīng)該使用SharedPreferences?APIs。一個SharedPreferences對象指向一個包含鍵值對的文件,并且提供了一些簡單的方法來進(jìn)行讀取和寫入它們。每個SharedPreferences文件可以是私有的或是共享的,靠framework來管理它們。

This class shows you how to use the SharedPreferences APIs to store and retrieve simple values.

這節(jié)課演示如何使用SharedPreferences?APIs來存儲和檢索簡單的值。

Note: The SharedPreferences APIs are only for reading and writing key-value pairs and you should not confuse them with thePreference APIs, which help you build a user interface for your app settings (although they useSharedPreferences as their implementation to save the app settings). For information about using thePreference APIs, see theSettings guide.

注意:SharedPreferences?APIs不僅僅可以讀取和寫入鍵值對,而且你不應(yīng)該把它們和Preference APIs相混淆,后者幫助你為你的app設(shè)置建立一個用戶接口(盡管在存儲app設(shè)置時它們使用SharedPreferences實現(xiàn))。

?

Get a Handle to a SharedPreferences ——得到一個SharedPreferences的句柄


You can create a new shared preference file or access an existing one by calling one of two methods:

  • getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from anyContext in your app.
  • getPreferences() — Use this from anActivity if you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don't need to supply a name.

你可以通過調(diào)用下面兩個方法之一創(chuàng)建一個新的共享引用文件或進(jìn)入一個已經(jīng)存在的文件:

  • getSharedPreferences()——如果你需要多個 依靠名字(第一個參數(shù))定義的共享引用文件,你可以使用這個函數(shù)。你可以從你的app中的任意Context里調(diào)用它。
  • getPreferences()——如果你在這個activity中僅僅需要使用一個共享應(yīng)用文件,就使用這個方法。因為它檢索一個屬于這個activity的默認(rèn)共享應(yīng)用文件,你不需要提供一個名稱。

For example, the following code is executed inside a Fragment. It accesses the shared preferences file that's identified by the resource stringR.string.preference_file_key and opens it using the private mode so the file is accessible by only your app.

例如,下面的代碼在一個Fragment中執(zhí)行。它依靠資源字符R.string.preference_file_key定義來訪問一個共享應(yīng)用文件,然后使用私有模式打開它,以使文件僅僅對你的app是可訪問的。

Context context = getActivity(); SharedPreferences sharedPref = context.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);

When naming your shared preference files, you should use a name that's uniquely identifiable to your app, such as"com.example.myapp.PREFERENCE_FILE_KEY"

Alternatively, if you need just one shared preference file for your activity, you can use thegetPreferences() method:

當(dāng)給你的共享引用文件命名時,你應(yīng)該使用一個唯一的、可確認(rèn)的名字,例如“com.example.myapp.PREFERENCE_FILE_KEY”。或者,如果你僅僅需要一個共享引用文件,你可以使用getPreferences()方法。

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

Caution: If you create a shared preferences file withMODE_WORLD_READABLE orMODE_WORLD_WRITEABLE, then any other apps that know the file identifier can access your data.

警告:如果你使用MODE_WORLD_READABLE 或者M(jìn)ODE_WORLD_WRITEABLE ,那么其他任何apps都知道這個文件標(biāo)識符可以訪問你的數(shù)據(jù)。

?

Write to Shared Preferences —— 向共享引用寫入數(shù)據(jù)


To write to a shared preferences file, create a SharedPreferences.Editor by callingedit() on yourSharedPreferences.

為了向一個共享的應(yīng)用文件寫入,通過在你的SharedPreferences調(diào)用edit() 來創(chuàng)建一個SharedPreferences.Editor。

Pass the keys and values you want to write with methods such as putInt() andputString(). Then callcommit() to save the changes. For example:

利用putInt()?和putString()這樣的方法傳遞你想要寫入的鍵和值。然后調(diào)用commit()來存儲變化。例如:

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

?

Read from Shared Preferences —— 從共享引用中讀取數(shù)據(jù)


To retrieve values from a shared preferences file, call methods such as getInt() andgetString(), providing the key for the value you want, and optionally a default value to return if the key isn't present. For example:

為了從一個共享引用文件中檢索數(shù)值,調(diào)用getInt() andgetString()等方法,提供一個你想到得到的鍵,和一個供選擇的默認(rèn)返回值(如果這個鍵不存在的話)。例如:

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);

?

轉(zhuǎn)載于:https://www.cnblogs.com/xiaowangba/archive/2013/02/04/6314740.html

總結(jié)

以上是生活随笔為你收集整理的[Andriod官方训练教程]保存数据之保存键-值对的集合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。