想要儲存一些簡單的資料,
可以使用SharedPreferences來儲存,
假設現在有三個欄位, 分別是名字, 電話和性別,
我不想要離開程式以後, 資料就消失了,
我可以把這些數值利用SharedPreferences存起來。

 

 
 




首先xml設計好三個欄位,以及三個按鈕,
方便驗證我們是否有將資料存入

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Name"
    />
    <EditText
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:singleLine="true"
     android:hint="givemepass"
     android:id="@+id/nameField"
    />
    <TextView  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Phone"
    />
    <EditText
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:singleLine="true"
     android:hint="0987654321"
     android:id="@+id/phoneField"
    />
    <TextView  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Sex"
    />
    <EditText
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:singleLine="true"
     android:hint="Male/Female"
     android:id="@+id/sexField"
    />
    <Button 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Save"
     android:id="@+id/saveButton"
    />
    <Button 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Clear"
     android:id="@+id/clearButton"
    />
    <Button 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="Read"
     android:id="@+id/readButton"
    />
</LinearLayout>


其中hint屬性可以提示使用者該輸入什麼值


接著我們將元件都初始化


    public void initComponent(){
        name = (EditText)findViewById(R.id.nameField);
        phone = (EditText)findViewById(R.id.phoneField);
        sex = (EditText)findViewById(R.id.sexField);
        save = (Button)findViewById(R.id.saveButton);
        read = (Button)findViewById(R.id.readButton);
        clear = (Button)findViewById(R.id.clearButton);
    }


設定好每個欄位存在sharedPreferences裡面的名稱

    private SharedPreferences settings;
    private static final String data = "DATA";
    private static final String nameField = "NAME";
    private static final String phoneField = "PHONE";
    private static final String sexField = "SEX";


接著我們設定存取資料的函式

    public void readData(){
        settings = getSharedPreferences(data,0);
        name.setText(settings.getString(nameField, ""));
        phone.setText(settings.getString(phoneField, ""));
        sex.setText(settings.getString(sexField, ""));
    }
    public void saveData(){
        settings = getSharedPreferences(data,0);
        settings.edit()
            .putString(nameField, name.getText().toString())
            .putString(phoneField, phone.getText().toString())
            .putString(sexField, sex.getText().toString())
            .commit();
    }


settings是SharedPreferences的物件,
我們首先在saveData裡面建立一個叫做"DATA"資料區,
然後將每個欄位的資料存進去,
接著readData把每個欄位的資料取出來,
放進所對應的editText裡面。

所以我們事件就幾乎是寫好了

    public void setEventListener(){
        save.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                saveData();
            }
        });
        read.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                readData();
            }
        });
        clear.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                name.setText("");
                phone.setText("");
                sex.setText("");
            }
        });
    }


save負責寫入, read負責讀出, clear則是把EditText上面的文字全部清除

因此我們就可以驗證, 當我們輸入好資料以後,
按下save在按下clear然後按下read,資料就全部恢復原狀。


程式碼下載
http://uploadingit.com/file/dynjofirz22caxz3/SharedPreferencesDemo.zip

arrow
arrow
    全站熱搜

    Felix 發表在 痞客邦 留言(0) 人氣()