Android Saving Data(一)

來源:互聯網
上載者:User

標籤:over   isp   image   sed   instance   更改   eof   寫入   tin   

Saving Key-value Sets  儲存索引值對

SharedPreferences只能用來儲存一些簡單的資料,並且這些資料可以是共用的,也可以是私人的。

SharedPreferences沒有構造方法,只能同個Context中的getSharePreference獲得。

擷取共用喜好設定的控制代碼

您可以通過調用以下兩種方法之一建立新的共用喜好設定檔案或訪問現有的檔案:

  • getSharedPreferences() — 如果您需要按照您用第一個參數指定的名稱識別的多個共用喜好設定檔案,請使用此方法。 您可以從您的應用中的任何 Context 調用此方法。
  • getPreferences() — 如果您只需使用 Activity 的一個共用喜好設定,請從 Activity 中使用此方法。 因為此方法會檢索屬於該 Activity 的預設共用喜好設定檔案,您無需提供名稱。

例如,以下代碼在 Fragment 內執行。它訪問通過資源字串 R.string.preference_file_key 識別的共用喜好設定檔案並且使用專用模式開啟它,從而僅允許您的應用訪問檔案。

例如,以下代碼在 Fragment 內執行。它訪問通過資源字串 R.string.preference_file_key 識別的共用喜好設定檔案並且使用專用模式開啟它,從而僅允許您的應用訪問檔案。

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

命名您的共用喜好設定檔案時,您應使用對於您的應用而言唯一可識別的名稱,比如 "com.example.myapp.PREFERENCE_FILE_KEY"

或者,如果您只需 Activity 的一個共用喜好設定檔案,您可以使用 getPreferences() 方法:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
寫入共用喜好設定

要寫入共用喜好設定檔案,請通過對您的 SharedPreferences 調用 edit() 來建立一個 SharedPreferences.Editor

傳遞您想要使用諸如 putInt()putString() 方法寫入的鍵和值。然後,調用 commit() 以儲存所做的更改。例如:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();
從共用喜好設定讀取資訊

要從共用喜好設定檔案中檢索值,請調用諸如 getInt()getString() 等方法,為您想要的值提供鍵,並根據需要提供要在鍵不存在的情況下返回的預設值。例如:

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


代碼:
public class MainActivity extends Activity {    TextView view;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.fragment_layout);        view=(TextView) findViewById(R.id.textView1);        Button write=(Button) findViewById(R.id.button1);        write.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                SharedPreferences sp=getPreferences(MODE_PRIVATE);                Editor editor=sp.edit();                editor.putInt(getString(R.string.flag), 1);                editor.commit();            }        });        Button read=(Button) findViewById(R.id.button2);        read.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                SharedPreferences sp=getPreferences(MODE_PRIVATE);                int defalt=-1;                int res=sp.getInt(getString(R.string.flag), defalt);                view.setText(String.valueOf(res));            }        });                    }}
View Code

 

Android Saving Data(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.