自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android ApiDemo示例解讀9:Activity->Persistent State

移動(dòng)開(kāi)發(fā) Android
App->Activity->Persistent State使用了Shared Preferences來(lái)保持部分UI狀態(tài)(TextView的值)。創(chuàng)建或是修改Shared Preferences,使用getSharedPreferences(String name, int mode)方法。Shared Preferences 用于單個(gè)Application不同Activity之間共享一些數(shù)據(jù),單不能用于不同Application之間共享數(shù)據(jù)。

Android 提供了多種存儲(chǔ)數(shù)據(jù)的方法,其中最簡(jiǎn)單的是使用Shared Preferences。Shared Preferences 可以存儲(chǔ) Key/value 對(duì),Shared Preferences 支持存取 boolean, float ,long ,integer, string ,最常用的使用Shared Preferences是用來(lái)存儲(chǔ)一些應(yīng)用偏好。此外的一個(gè)方法是使用onSaveInstanceState(),這是特別用來(lái)保存UI 狀態(tài)的。

App->Activity->Persistent State使用了Shared Preferences來(lái)保持部分UI狀態(tài)(TextView的值)。

創(chuàng)建或是修改Shared Preferences,使用getSharedPreferences(String name, int mode)方法。Shared Preferences 用于單個(gè)Application不同Activity之間共享一些數(shù)據(jù),單不能用于不同Application之間共享數(shù)據(jù)。

SharedPreferences.Editor 用來(lái)給Shared Preferences添加數(shù)據(jù): editor.putXXX(key,value)

  1. protected void onPause() {   
  2. super.onPause();   
  3.     
  4. SharedPreferences.Editor editor = getPreferences(0).edit();   
  5. editor.putString("text", mSaved.getText().toString());   
  6. editor.putInt("selection-start", mSaved.getSelectionStart());   
  7. editor.putInt("selection-end", mSaved.getSelectionEnd());   
  8. editor.commit();   
  9. }   

讀取Shared Preference: pref.getXXX(key)

  1. protected void onResume() {   
  2. super.onResume();   
  3.     
  4. SharedPreferences prefs = getPreferences(0);   
  5. String restoredText = prefs.getString("text"null);   
  6. if (restoredText != null) {   
  7. mSaved.setText(restoredText, TextView.BufferType.EDITABLE);   
  8.     
  9. int selectionStart = prefs.getInt("selection-start", -1);   
  10. int selectionEnd = prefs.getInt("selection-end", -1);   
  11. if (selectionStart != -1 && selectionEnd != -1) {   
  12. mSaved.setSelection(selectionStart, selectionEnd);   
  13. }   
  14. }   
  15. }   

Android ApiDemo示例解讀系列之九:App->Activity->Persistent State

Persistent State 演示了如何使用Shared Preferences在Activity 恢復(fù)時(shí)保持EditText的內(nèi)容。 單是更一般的方法是使用onSaveInstanceState。

責(zé)任編輯:閆佳明 來(lái)源: jizhuomi
相關(guān)推薦

2013-12-19 14:32:31

Android ApiAndroid開(kāi)發(fā)Android SDK

2013-12-19 14:34:52

Android ApiAndroid開(kāi)發(fā)Android SDK

2013-12-19 14:16:46

Android ApiAndroid開(kāi)發(fā)Android SDK

2013-12-19 14:13:16

Android ApiAndroid開(kāi)發(fā)Android SDK

2013-12-19 14:28:04

Android ApiAndroid開(kāi)發(fā)Android SDK

2013-12-19 14:00:39

Android ApiAndroid開(kāi)發(fā)Android SDK

2013-12-19 13:40:44

Android ApiAndroid開(kāi)發(fā)Android SDK

2013-12-19 13:51:12

Android ApiAndroid開(kāi)發(fā)Android SDK

2013-12-19 16:26:29

Android ApiAndroid開(kāi)發(fā)Android SDK

2014-05-27 15:07:07

AndroidActivitysingleTask

2010-02-02 14:22:50

Python示例

2010-02-01 11:22:09

C++虛函數(shù)

2010-03-02 14:41:00

WCF行為控制

2010-03-05 10:47:05

Python futu

2017-03-06 17:15:34

ARVR交互方式

2013-01-08 13:33:07

Android開(kāi)發(fā)Activity入門(mén)指南

2015-10-20 15:54:16

android源碼滑動(dòng)關(guān)閉

2012-02-17 17:07:30

Android安全Activity劫持

2010-02-04 16:07:39

C++回調(diào)函數(shù)

2010-01-04 17:03:27

Silverlight
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)