成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

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

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

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

SharedPreferences.Editor 用來給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ù)時保持EditText的內(nèi)容。 單是更一般的方法是使用onSaveInstanceState。

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

2013-12-19 14:32:31

Android ApiAndroid開發(fā)Android SDK

2013-12-19 14:13:16

Android ApiAndroid開發(fā)Android SDK

2013-12-19 14:16:46

Android ApiAndroid開發(fā)Android SDK

2013-12-19 14:34:52

Android ApiAndroid開發(fā)Android SDK

2013-12-19 14:00:39

Android ApiAndroid開發(fā)Android SDK

2013-12-19 14:28:04

Android ApiAndroid開發(fā)Android SDK

2013-12-19 13:40:44

Android ApiAndroid開發(fā)Android SDK

2013-12-19 13:51:12

Android ApiAndroid開發(fā)Android SDK

2013-12-19 16:26:29

Android ApiAndroid開發(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交互方式

2010-02-04 16:07:39

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

2010-01-04 17:03:27

Silverlight

2013-01-08 13:33:07

Android開發(fā)Activity入門指南

2015-10-20 15:54:16

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

2012-02-17 17:07:30

Android安全Activity劫持
點贊
收藏

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

主站蜘蛛池模板: www视频在线观看 | 巨大黑人极品videos精品 | 久久久久国产精品一区二区 | 中文在线视频 | 国产99久久久久 | 自拍偷拍第一页 | 人人澡人人射 | 电影在线 | 色.com| 国产成人一区在线 | 99热欧美| 亚洲欧洲在线看 | www.成人.com | 97人人澡人人爽91综合色 | 午夜影院| 欧美区日韩区 | 中文字幕av网 | аⅴ资源新版在线天堂 | 香蕉av免费 | www.亚洲视频.com| 日本三级网站在线观看 | 精品欧美一区二区在线观看欧美熟 | 婷婷在线免费 | 亚洲xx在线| 亚洲国产精品一区二区第一页 | 中文字幕在线三区 | 久久精片| 日本一区二区三区在线观看 | 91视频大全 | av色站| 国产精品一区二区三 | 毛片在线免费 | 欧美精品久久久 | 69热视频在线观看 | 亚洲欧美精品在线 | 久久久久国产 | 国产一区二区三区四区hd | 国产精产国品一二三产区视频 | 久久久久91 | 成人欧美一区二区三区 | 国产日韩一区二区 |