寫入資料

SharedPreferences spToDo = getSharedPreferences("prefile",MODE_PRIVATE);
spToDo.edit().putString("username","superman").commit();

放進資料的方式有putBoolean,putFloat,putInt,putLong,putString,依需求自已改變.
而檔案權限分為
MODE_PRIVATE 只有本應用程式有存取權限
MODE_WORLD_READABLE 所有應用程式都有讀取權限
MODE_WORLD-WRITEABLE 所有應用程式都有寫入權限

讀取資料

SharedPreferences spToDo = getSharedPreferences("prefile",MODE_PRIVATE);
String readValue = spToDo.getString("username","unknow");

移除資料

SharedPreferences spToDo = getSharedPreferences("prefile",MODE_PRIVATE);
spToDo.edit().remove("username").commit();

清空資料

SharedPreferences spToDo = getSharedPreferences("prefile",MODE_PRIVATE);
spToDo.edit().clear().commit();

簡單的例子

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edt1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
    <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="write to spFile"
android:id="@+id/btn1"
android:layout_below="@+id/edt1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
    <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv_spReadShow"
android:layout_below="@+id/btn3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
    <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="read from spfile"
android:id="@+id/btn2"
android:layout_below="@+id/tv_spReadShow"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
    <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="delete in spfile"
android:id="@+id/btn3"
android:layout_below="@+id/edt1"
android:layout_toRightOf="@+id/btn1"
android:layout_toEndOf="@+id/btn1" />
    <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clear spfile"
android:id="@+id/btn4"
android:layout_alignTop="@+id/btn2"
android:layout_toRightOf="@+id/btn2"
android:layout_toEndOf="@+id/btn2" />
</RelativeLayout>

MainActivity.java

package tw.idv.charleslin74.sharedpreferencestest;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    EditText edt1;
    TextView tv_spReadShow;
    Button btn1,btn2,btn3,btn4;
    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edt1 = (EditText)findViewById(R.id.edt1);
        tv_spReadShow = (TextView)findViewById(R.id.tv_spReadShow);
        btn1 = (Button)findViewById(R.id.btn1);
        btn2 = (Button)findViewById(R.id.btn2);
        btn3 = (Button)findViewById(R.id.btn3);
        btn4 = (Button)findViewById(R.id.btn4);
        btn1.setOnClickListener(btnClickListener);
        btn2.setOnClickListener(btnClickListener);
        btn3.setOnClickListener(btnClickListener);
        btn4.setOnClickListener(btnClickListener);
    }
    private Button.OnClickListener btnClickListener = new Button.OnClickListener() {
        @Override
public void onClick(View v) {
            SharedPreferences spToDo = getSharedPreferences("prefile", MODE_PRIVATE);
            String inputValue;
            switch (v.getId()) {
                case R.id.btn1:
                    inputValue = String.<em>valueOf</em>(edt1.getText());
                    spToDo.edit().putString("username", inputValue).commit();
                    break;
                case R.id.btn2:
                    String readValue = spToDo.getString("username", "unknow");
                    tv_spReadShow.setText(readValue);
                    break;
                case R.id.btn3:
                    inputValue = String.<em>valueOf</em>(edt1.getText());
                    spToDo.edit().remove("username").commit();
                    break;
                case R.id.btn4:
                    spToDo.edit().clear().commit();
                    break;
            }
        }
    };
}
arrow
arrow
    創作者介紹
    創作者 痞客興 的頭像
    痞客興

    痞客興的部落格

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