Android輕量級數據保存方式

  簡單存儲指的是Android系統提供的輕量級的數據保存方式SharedPreferences,將數據以最簡單的方式進行永久的存儲,SharedPreferences屏蔽了對底層的文件操作,爲程序員提供簡單的程序接口,實現基於關鍵字的數據保存。

  在使用SharedPreferences前,先定義SharedPreferences的訪問模式。(SharedPreferences支持3種訪問模式:私有(MODE_PRIVATE)、全局讀(MODE_WORLD_READABLE)、全局寫(MODE_WORLD_WRITEABLE))。

 

public static int MODE = MODE_PRIVATE;

  除了定義SharedPreferences的訪問模式,還需要定義SharedPreferences的名稱

 

 public static final String PREFERENCE_NAME = "SaveSetting";

  使用SharedPreferences時需要將訪問模式和SharedPreferences名稱作爲參數傳遞到getSharedPreferences()函數,則可獲取SharedPreferences實例。

 

SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);

  在獲取SharedPreferences實例後,可以通過SharedPreferences.Editor類對SharedPreferences進行修改,然後調用commit()函數保存修改內容。

 

SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString("Name", nameText.getText().toString());
		editor.putInt("Age", Integer.parseInt(ageText.getText().toString()));
		editor.putFloat("Height",
				Float.parseFloat(heightText.getText().toString()));
		editor.commit();

  如果需要從保存的SharedPreferences中讀取數據,同樣調用getSharedPreferences()函數。

SharedPreferences sharedPreferences = getSharedPreferences(
				PREFERENCE_NAME, MODE);
		String name = sharedPreferences.getString("Name", "Tom");
		int age = sharedPreferences.getInt("Age", 20);
		float height = sharedPreferences.getFloat("Height", 1.78f);


源碼:

MainActivity.java

public class MainActivity extends Activity {
	private EditText nameText, ageText, heightText;
	//定義SharedPreferences的名稱
	public static final String PREFERENCE_NAME = "SaveSetting";
	//定義SharedPreferences的訪問模式:全局讀+全局寫
	public static int MODE = Context.MODE_WORLD_READABLE
			+ Context.MODE_WORLD_WRITEABLE;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		nameText = (EditText) findViewById(R.id.editText1);
		ageText = (EditText) findViewById(R.id.editText2);
		heightText = (EditText) findViewById(R.id.editText3);
	}

	@Override
	protected void onStart() {		
		loadSharedPreferences();
		super.onStart();
	}

	@Override
	protected void onStop() {
		saveSharedPreferences();
		super.onStop();
	}

	private void loadSharedPreferences() {
		SharedPreferences sharedPreferences = getSharedPreferences(
				PREFERENCE_NAME, MODE);
		String name = sharedPreferences.getString("Name", "Tom");
		int age = sharedPreferences.getInt("Age", 20);
		float height = sharedPreferences.getFloat("Height", 1.78f);
		//讀取數據
		nameText.setText(name);
		ageText.setText(String.valueOf(age));
		heightText.setText(String.valueOf(height));
	}

	private void saveSharedPreferences() {
		SharedPreferences sharedPreferences = getSharedPreferences(
				PREFERENCE_NAME, MODE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putString("Name", nameText.getText().toString());
		editor.putInt("Age", Integer.parseInt(ageText.getText().toString()));
		editor.putFloat("Height",
				Float.parseFloat(heightText.getText().toString()));
		editor.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

佈局文件 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="39dp"
        android:text="姓名:" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="43dp"
        android:text="年齡:" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignLeft="@+id/editText1"
        android:ems="10" >
    </EditText>

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignLeft="@+id/editText2"
        android:ems="10" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="42dp"
        android:text="身高:" />

</RelativeLayout>



SaveSetting.xml保存在/data/data/<package name>/shared_prefs目錄下。


效果圖:

修改信息,退出再次進入即可看到保存的信息。。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章