天天看點

Android Api Demos登頂之路(十一)Persistent state

這個demo示範了利用 SharedPreferences進行資料的持久化。

在本例中定義了兩個文本框,對第一個文本框實作了對資料的内容和選中狀态的持久化

是以當我們對文本框中的内容進行了修改,或是選中了某些文本,即使我們此時退出了程式,當我們再次打開程式時,我們依然會得到我們修改後的内容,或是某些文本的選中狀态。

在第二個文本框中我們并沒有實作持久化,是以當我們退出後再次進入應用時,我們之前對文本框中的内容或選中狀态所做的修改就全部丢失了,文本框又恢複到了初始的狀态。

這裡需要注意的是:SharedPreferences 類,它是一個輕量級的存儲類,特别适合用于儲存軟體配置參數。

擷取SharedPreferences對象方法:有三種

1.SharedPreferences pre = Context.getSharedPreferences(String name,int mode);

2.SharedPreferences pre = Activity.getPreferences(int mode);

3.SharedPreferences pre = PreferenceManager.getDefaultSharedPreferences(Context);

本例使用的是第二種。

布局檔案:activity_persistent_state.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:orientation="vertical" >

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Demonstration of persistent activity state with getPreferneces(0).edit() and getPreferneces(0)."/>

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginTop="10dp"
        android:text="This text field save its state"/>
    <EditText 
        android:id="@+id/et_persistent_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:text="Initial text"
        android:background="#00ff00"/>
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginTop="10dp"
        android:text="This text field does not save its state"/>
    <EditText 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:text="Initial text"
        android:background="#ff0000"/>
</LinearLayout>
           

PersistentStateActivity類

public class PersistentStateActivity extends Activity {
    private EditText et_save;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_persistent_state);
        et_save = (EditText) findViewById(R.id.et_persistent_save);
        //MODE_PRIVATE: 私有方式存儲,其他應用無法通路
        sp = getPreferences(MODE_PRIVATE);
    }

    /**
     * 當activity不可見時儲存EditText的狀态
     */
    @Override
    protected void onPause() {
        super.onPause();
        // 擷取SharedPreferences的編輯器
        Editor editor = sp.edit();
        // 向SharedPreferences中存儲文本和選中的狀态
        editor.putString("text", et_save.getText().toString());
        editor.putInt("selection_start", et_save.getSelectionStart());
        editor.putInt("selection_end", et_save.getSelectionEnd());
        // 送出資料,注意存儲完成後一定要注意送出資料,否則資料不會被儲存到SharedPreferences當中
        editor.commit();
    }

    /**
     * 當activity恢複時恢複EditText的狀态
     */

    @Override
    protected void onResume() {
        super.onResume();
        // 從SharedPreferences中取出我們存入的文本和選中狀态資訊
        String text = sp.getString("text", null);
        if (text!= null && text!="") {
            //TextView.BufferType.EDITABLE允許在原來顯示的檔案的基礎上追加文本,其實預設的狀态下就可以
            //追加文本,即使我們不設定這個參數
            et_save.setText(text,TextView.BufferType.EDITABLE);
            // 設定預設值為-1
            int start = sp.getInt("selection_start", -);
            int end = sp.getInt("selection_end", -);
            if (start != - && end != -) {
                et_save.setSelection(start, end);
            }
        }
    }
}
           

繼續閱讀