现在的位置: 首页 > 综合 > 正文

用SharedPreferences存储数据测试

2014年08月09日 ⁄ 综合 ⁄ 共 3735字 ⁄ 字号 评论关闭

SharedPreferences类是Android平台上的一个轻量级数据存储类。可以用来存储数据,通过键值对的方式存取数据。存储的数据将保存在/data/data/<包名>/shared_prefs目录下。SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。实现SharedPreferences存储的步骤如下:

1)用Context获取SharedPreferences对象

2)利用edit()方法获取Editor对象。

3)通过Editor对象存储key-value键值对数据。

4)通过commit()方法提交数据。


     下面的程序将演示在一个android界面中用SharedPreferences类在第一个界面存储一个数据,切换到第二个界面后取出数据。

第一个页面布局

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />


    <Button
        android:id="@+id/b1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="切换页面" />

</LinearLayout>

mainActivity代码

package ss.ss.namespace;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class mainActivity extends Activity {
	SharedPreferences sp;
	private Button b1;
	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Context ctx = mainActivity.this;
		sp = ctx.getSharedPreferences("SP", MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString("STRING_KEY", "string");
		editor.putInt("INT_KEY", 0);
		editor.putBoolean("BOOLEAN_KEY", true);
		editor.commit();
		
		Log.d("SP", sp.getString("STRING_KEY", "none"));
	    Log.d("SP", sp.getString("NOT_EXIST", "none"));
	    
	    
	    b1 = (Button) findViewById(R.id.b1);
	    b1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mainActivity.this,secondActivity.class);
				startActivity(intent);
			}
		});
	    
	}
}


第二个页面布局

<?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:orientation="vertical" >


    <Button
        android:id="@+id/b2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取SP.XML值" />

    <EditText
        android:id="@+id/eT1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>


    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回" />

</LinearLayout>

secondActivity代码

package ss.ss.namespace;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class secondActivity extends Activity {
	SharedPreferences sp;	
	private Button b1;
	private Button b2;
	EditText eT1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		b1 = (Button) findViewById(R.id.b2);
		b2 = (Button) findViewById(R.id.b3);
		eT1 = (EditText) findViewById(R.id.eT1);
		
		b1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Context ctx = secondActivity.this;
				SharedPreferences sp = ctx.getSharedPreferences("SP",MODE_PRIVATE );
				
				eT1.setText(sp.getString("STRING_KEY", "none"));
				
			}
		});
		
		b2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();//返回前一界面
			}
		});
	}
	

}

运行结果如下:



查看sp.xml文件如下


参考文章:http://www.cnblogs.com/wisekingokok/archive/2011/09/16/2177833.html

抱歉!评论已关闭.